Handling Databases with PHP
Session 15
Review
A variable can store only one value at a time
An array is a variable that can store a set of values of the same
data type
We can combine the element values of two or more arrays. This
process is called as merging arrays
In a single-dimensional array, the element includes only one
level of key value pairs
In the multidimensional array, each element is an array. Each
element requires an array name and multiple set of indices
Multidimensional arrays are arrays that store another array within
it
PHP / Session 15 / Slide 2 of 24
Objectives
Database APIs
Connecting to a database
Data access functions
Performing SQL queries using PHP
Building HTML tables using SQL queries
PHP / Session 15 / Slide 3 of 24
Database APIs
Allows developers to write applications those are movable or
easily accessible between the database products
Creates a common set of libraries for the program when used
with programs
Native-Interface, ODBC, JDBC, and CORBA are the
common database APIs
PHP supports MySQL database for accessing data from the
database server
MySQL does not have its own APIs
PHP / Session 15 / Slide 4 of 24
Connecting to a Database
Uses three arguments to connect PHP to MySQL
such as server host name, user name, user
password
Connectivity of PHP to MySQL are divided into
three steps:
Connection with the MySQL server
Working with the databases
Closing the database connection
PHP / Session 15 / Slide 5 of 24
Connection with the MySQL
server - I
Connects with the MySQL server using
mysql_connect() function
This function takes three arguments:
Hostname
Database username
Database user password
PHP / Session 15 / Slide 6 of 24
Connection with the MySQL
server - II
Syntax for connecting with the MySQL server is:
$link_id = mysql_connect(“host_name”,
“user_name”,“password”);
Where,
host_name – Specifies the server or the host name
user_name – Specifies the user name of MySQL
password – Specifies the password for the MySQL user
link_id - Specifies the return value of the server
connection
PHP / Session 15 / Slide 7 of 24
Example for connection with the
MySQL server
For example, to connect PHP to MySQL server
$server = “”;
$username = “root”;
$password = “”;
$connect_mysql = mysql_connect($server,
$username, $password);
echo “Connected to MySQL”;
?>
PHP / Session 15 / Slide 8 of 24
Working with the Databases
Establish a connection with the MySQL server
Needs to perform basic PHP functions while
working with the database such as
mysql_list_dbs()
mysql_select_db()
mysql_list_tables()
mysql_num_rows()
PHP / Session 15 / Slide 9 of 24
mysql_list_dbs() function
Displays all the databases present in the server
Syntax for this function is:
$result =
mysql_list_dbs($link_id);
Where,
link_id - Specifies the return value of the
server connection
result – Assigns the value of the function
PHP / Session 15 / Slide 10 of 24
mysql_select_db() function
Selects a database from the server
Syntax for this function is:
$result = mysql_select_db(“database_name”,
$link_id);
Where,
database_name – Specifies the name of the
database
link_id – Specifies the return value of the server
connection
result – Assigns the value of the function
PHP / Session 15 / Slide 11 of 24
mysql_list_tables() function
Displays the list of tables present in the selected database
Syntax for this function is:
$result = mysql_list_tables(“database_name”,
$link_id);
Where,
database_name – Specifies the database name
link_id – Specifies the return value of the database
connection
result – Stores the result of the function
PHP / Session 15 / Slide 12 of 24
mysql_num_rows() function
Shows the number of rows present in the table
Syntax for this function is:
$num_rows = mysql_num_rows($result);
Where,
result – Specifies the argument taken for
displaying the number of rows
num_rows – Stores the result of the function
PHP / Session 15 / Slide 13 of 24
Closing the connection
Closes the connection with the MySQL server by
using the mysql_close() function
Syntax for this function is :
mysql_close($link_id);
Where,
link_id - Specifies the return value of the server
connection
PHP / Session 15 / Slide 14 of 24
Data Access Functions
Uses MySQL functions in PHP for accessing data
from the tables of the database
Data are accessed from the tables using the following
mysql functions. Those are:
mysql_query()
mysql_fetch_array()
mysql_fetch_row()
mysql_fetch_field()
mysql_field_len()
mysql_num_fields()
PHP / Session 15 / Slide 15 of 24
Example for data access functions
-I
For example, to display the records of the table from the
USER database using the data access functions
$server = “”;
$username = “root”;
$password = “”;
$connect_mysql = mysql_connect($server,
$username, $password);
if($connect_mysql)
echo “Connection established”;
else
die(“Unable to connect”);
PHP / Session 15 / Slide 16 of 24
Example for data access functions
- II
$mysql_db = mysql_select_db(“USER”);
if($mysql_db)
echo “Connected to the database”;
else
die(“Unable to connect to database”);
$sqlquery = mysql_query(“SELECT * FROM
USER_DETAILS WHERE ADDRESS = ‘CALIFORNIA’”);
while($row = mysql_fetch_array($sqlquery))
{
echo “Name:”.$row[USER_NAME];
echo “Phone-No:”.$row[USER_PHONE_NO];
echo “Address:”.$row[USER_ADDRESS];
}
PHP / Session 15 / Slide 17 of 24
Example for data access functions
- III
if(mysql_num_rows($sqlquery)<1)
{
echo “No result”;
}
?>
The above codes will display the records of the USER_DETAILS
table from the USER database
PHP / Session 15 / Slide 18 of 24
Performing SQL queries using
PHP - I
For example, create a table using SQL commands in PHP scripts
$server = “”;
$username = “root”;
$password = “”;
$connect_mysql = mysql_connect($server, $username,
$password);
if($connect_mysql)
echo “Connection established”;
else
die(“Unable to connect”);
PHP / Session 15 / Slide 19 of 24
Performing SQL queries using
PHP - II
$mysql_db = mysql_select_db(“USER”);
if($mysql_db)
echo “Connected to the database”;
else
die(“Unable to connect to database”);
$sql_table=”CREATE TABLE USER_CONTACT(“.“USER_ID
INT NOT NULL PRIMARY KEY,“.“USER_NAME CHAR(25)
NOT NULL,“.“USER_EMAIL_ID CHAR(25)”. “)”;
if($result=mysql_query($sql_table))
echo “Table is created”;
else
die(“Unable to create a table”);
?>
PHP / Session 15 / Slide 20 of 24
Building HTML tables using SQL
queries
HTML supports the database application components
for accessing the database
For example, to display all the records of the
user_contact table from the user database by
using HTML table structure
PHP / Session 15 / Slide 21 of 24
Example for HTML tables using
SQL queries - I
<HTML><BODY>
$server = “”;
$username = “root”;
$password = “”;
$connect_mysql = mysql_connect($server,
$username, $password);
if($connect_mysql)
echo “Connection established”;
$mysql_db = mysql_select_db(“USER”);
PHP / Session 15 / Slide 22 of 24
Example for HTML tables using
SQL queries - II
if($mysql_db)
echo “<BR>Connected to the database”;
echo “<TABLE BORDER BGCOLOR=“WHITE”>”;
echo “<TR><TH> USER_ID <TH><TH> USER_NAME
<TH><TH> USER_EMAIL_ID </TH>”;
echo “<DBQUERY q> select * from user_contact”;
echo “<DBROW><TR><TD><? q.USER_ID></TD><TD>
q.USER_NAME></TD><TD><? q.USER_EMAIL_ID>
</TD></TR>”;
echo “</DBQUERY>”;echo “</TR>”;echo “</TABLE>”;
?>
</BODY></HTML>
PHP / Session 15 / Slide 23 of 24
Summary
Database APIs allows the developers to write
applications that are movable or easily accessible
between the database products
Connection with the server is done by using
mysql_connect() function
Functions used with the database are
mysql_list_dbs(), mysql_select_db(),
mysql_list_tables(), mysql_num_rows()
Connection with the MySQL server is done with
mysql_close() function
PHP / Session 15 / Slide 24 of 24