Vous êtes sur la page 1sur 24

Handling Databases with PHP

Chapter 9
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 / Chapter 9 / Slide 2 of 27
Objectives
 Database APIs
 Connecting to a database
 Data access functions
 Performing SQL queries using PHP
 Building HTML tables using SQL queries

PHP / Chapter 9 / Slide 3 of 27


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 / Chapter 9 / Slide 4 of 27


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 / Chapter 9 / Slide 5 of 27


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 / Chapter 9 / Slide 6 of 27


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 / Chapter 9 / Slide 7 of 27


Example for connection with the
MySQL server
 For example, to connect PHP to MySQL server
<?php
$server = “”;
$username = “root”;
$password = “”;
$connect_mysql = mysql_connect($server,
$username, $password);
echo “Connected to MySQL”;
?>

PHP / Chapter 9 / Slide 8 of 27


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 / Chapter 9 / Slide 9 of 27


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 / Chapter 9 / Slide 10 of 27


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 / Chapter 9 / Slide 11 of 27
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 / Chapter 9 / Slide 12 of 27


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 / Chapter 9 / Slide 13 of 27


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 / Chapter 9 / Slide 14 of 27
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 / Chapter 9 / Slide 15 of 27


Example for data access functions
-I
 For example, to display the records of the table from the
USER database using the data access functions
<?php
$server = “”;
$username = “root”;
$password = “”;
$connect_mysql =
mysql_connect($server,$username, $password);
if($connect_mysql)
echo “Connection established”;
else
die(“Unable to connect”);
PHP / Chapter 9 / Slide 16 of 27
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 / Chapter 9 / Slide 17 of 27
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 / Chapter 9 / Slide 18 of 27


Performing SQL queries using
PHP - I
 For example, create a table using SQL commands in PHP scripts
<?php
$server = “”;
$username = “root”;
$password = “”;
$connect_mysql = mysql_connect($server, $username,
$password);
if($connect_mysql)
echo “Connection established”;
else
die(“Unable to connect”);

PHP / Chapter 9 / Slide 19 of 27


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 / Chapter 9 / Slide 20 of 27
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 / Chapter 9 / Slide 21 of 27


Example for HTML tables using
SQL queries - I
<HTML><BODY>
<?php
$server = “”;
$username = “root”;
$password = “”;
$connect_mysql = mysql_connect($server,
$username, $password);
if($connect_mysql)
echo “Connection established”;
$mysql_db = mysql_select_db(“USER”);
PHP / Chapter 9 / Slide 22 of 27
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 / Chapter 9 / Slide 23 of 27
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 / Chapter 9 / Slide 24 of 27

Vous aimerez peut-être aussi