PHPChapter (12!13!14) Databases
PHPChapter (12!13!14) Databases
2
Using PHP with a database
The steps to interact with a database are:
1. Connect to the database.
2. Send an SQL query that contains instructions for the
database software.
3. If you retrieved data from the database, process the data.
4. Close the connection to the database.
3
PHP and Databases
In order to make use of a data layer, you must have
access to a database.
You should have one through this course.
4
Connecting to the Database
To make the connection to MySQL database:
<?
$host=“localhost”;
$user=“root”;
$pass=“”;
$connection=mysql_connect($host,$user,$pass)
Or die (“Couldn’t connect to database”);
?>
5
Possible Errors
Depending on the level of reporting in your browser,
you may or may not get meaningful error messages if
this goes wrong.
However, if you have a problem it is one of the
following:
The host is not correct.
The username does not exist.
The password does not match the username.
6
A Database Connection
Having made the connection to the server, you must select the
database with which you are going to work.
<?<?php <<
$host=“localhost”;
$user=“root”;
$pass=“”;
$database=“mydb”;
$connection=mysql_connect($host,$user,$pass)
Or die (“Couldn’t connect to database”);
mysql_select_db($database, $connection);
?>
7
Example 1- Creating Database
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{ die('Could not connect: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE mydb",$con)) {
echo "Database created"; }
else { echo "Error creating database: " . mysql_error();
}
mysql_close($con); dbcreate1.php
8
?>
Example 1 - Creating a Table
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{ die('Could not connect: ' . mysql_error( ));
}
mysql_select_db("mydb", $con);
$ret=mysql_query($query,$con);
tablecreate.php
9
Example 1 - Creating a Table
if($ret) {
echo "<p>Table created!</p>";
}
else {
echo "<p>Something went wrong: ", mysql_error() +
"</p>";
}
mysql_close($con);
?>
10
Example 2-
Creating Database & Table
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Create database
if (mysql_query("CREATE DATABASE my_db1",$con))
{ echo "Database created";
}
else {
echo "Error creating database: " . mysql_error();
}
11
Example 2-
Creating Database & Table
// Create table
mysql_select_db("my_db1", $con);
// Execute query
mysql_query($sql,$con);
mysql_close($con);
?>
12
Example 3- Creating Table
<?php
mysql_select_db("my_db1", $con);
$sql = "CREATE TABLE User
(
UserID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(UserID),
Name varchar(15),
Address varchar(50),
Age int
)";
mysql_query($sql,$con); // Execute query
mysql_close($con);
?>
13
Inserting Data into a Table (1)
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db1", $con);
14
Inserting Data into a Table (1)
$ret=mysql_query($query,$con);
If($ret) {
echo "<p> User Table is inserted!</p>";
}
Else {
echo "<p>Something went wrong: ", mysql_error() +
"</p>";
}
mysql_close($con);
?>
15
Inserting Data into a Table (2)
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{ die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db1", $con);
17
Manipulating the Results
The results come out as an array of associative arrays.
Thus, making use of the data we have queried for the database
requires us to provide handling code in PHP.
18
Extracting the Data
PHP gives us a number of helper functions.
If we want how many records that were returned, we use the
mysql_num_rows function.
19
Extracting the Data
<?
$host=“localhost”;
$user=“”;
$pass=“”;
$database=“my_db1”;
$connection=mysql_connect($hosts,$user,$pass)or die(“Couldn’t connect to
datbase”);
mysql_select_db($database,$connection);
21
Fetching a Row
echo ”<table width=\”100%\” border=1>”;
echo ”<tr>”; Last Name Age
echo ”<th align=\”left\”>Last Name</th>”; Griffin 35
Smith 33
echo ”<th align=\”left\”>Age</th>”;
echo ”</tr>”;
for ($i=0; $i<$num_results; $i++)
{ $row = mysql_fetch_array($ret);
echo”<tr>”;
echo”<td>”.$row[“LastName”].”</td>”; //field name = key name
echo”<td>”.$row[“Age”].”</td>”;
echo”</tr>”;
}
22
User Interaction Form
<html>
<head>
<title>Database Form</title>
</head>
<body>
<form action=“database_search.php” method=“POST”>
<p>First Name</p>
<input type=“text” name=“firstname”>
<p>Last Name</p>
<input type=“text” name=“lastname”>
<input type=“reset” value=“clear values”>
<input type=“submit” value=“submit”>
</body>
</html>
23
Form Process
$firstname=$_POST[“firstname”];
$lastname=$_POST[“lastname”];
$ret=mysql_query($query,$connection);
$num_results=mysql_num_rows($ret);
24
Retrieving Data
$firstname=$_POST[“firstname”];
$lastname=$_POST[“lastname”];
$firstname=mysql_real_escape_string($firstname);
$lastname=mysql_real_escape_string($lastname);
$ret=mysql_query($query,$connection);
25
Updating Records
UPDATE <table_name> SET <field_name>=‘value’
WHERE <condition>
26
Deleting Records
27
Thank You!
28