PHP and Database Integration With MySQL
PHP and Database Integration With MySQL
- Chetan Akarte
One of the defining features of PHP is the versatility it offers for connection to, and manipulation with,
databases. n this article, we look at some features of the PHP and !y"#$ combination. %e shall go
through the following steps&-
Connect to MySQL Database Server
Create new Database
Select MySQL Database
Add data to table
Retrieve data
Error Handling
Connect to MySQL Database Server
'o work with the !y"#$ database, we first need to connect to !y"#$ (atabase "erver. PHP provides
mysql_connect! function to do this, and re)uires three strings as input& *hostname+, *username+ and
*password+. ,se Code"# to connect to !y"#$ (atabase "erver.
Code 1: test.php
<html>
<head>
<title>Server test Page </title>
</head>
<body>
<p>Testing working of MySQL.</p>
<php
/! "onne#ting to MySQL !/
$link % mys&l'#onne#t()mys&l'host)* )mys&l'+ser)* )mys&l'password), or
die()"o+ld not #onne#t - ) . mys&l'error(,,.
print )"onne#ted s+##essf+lly).
>
</body>
</html>
f your script has no error, then Code-- gives output as&
Output 1: test.php
Testing working of MySQL.
"onne#ted s+##essf+lly
Create ne$ Database
n the ne.t e.ample, we are creating a database %ser and under that, we are creating a table visitor. 'he
table visitor will be created with four columns& a primary key called *id+ that will be auto incremented as
data is added to the table, and the remaining three columns are the character /0A1CHA12 fields& *name+,
*address+ and *email+. Code"& will help you to create the table successfully.
Code 2 :- db.php
<html>
<head>
<title>"reate /atabase </title>
</head>
<body>
<p>"reating /atabase 0 table in MySQL.</p>
<php
// "onne#ting to MySQL
$link % mys&l'#onne#t()mys&l'host)* )mys&l'+ser)* )mys&l'password), or
die()"o+ld not #onne#t - ) . mys&l'error(,,.
print )"onne#ted s+##essf+lly<P>).
$/1 % )+ser).
$table % )visitor).
$&+ery % )"234T3 /4T414S3 $/1).
$res+lt % mys&l'&+ery($&+ery, or die()32252 while #reating
database).mys&l'error(,,.
print()56* database made* name of /1 - $/1<br><br>),.
mys&l'sele#t'db($/1* $link,.
$&+ery7 % )"234T3 T41L3 $table (id 89T 95T 9:LL 4:T5'89"23M39T P28M42;
63;* name var#har(7<,* address var#har(<=,* email var#har(7<,,).
$res+lt7 % mys&l'&+ery($&+ery7, or die()32252 while #reating
table).mys&l'error(,,.
print()56* table made* name of table - visitor<br><br>),.
>
</body>
</html>
f db'(h( runs without any error, it generates the output as shown in )%t(%t"&.
Output 2: db.php
Testing working of MySQL.
"onne#ted s+##essf+lly
56* database made* name of /1- +ser
56* table made* name of table - visitor
Select MySQL Database
3ow, we have established the connection with !y"#$, and creates database %ser and table visitor. 'o
work with the database, it must select. 'o select any !y"#$ database, PHP provides the
mysql_select_db! function. t re)uires the database name which is to be selected and the link to
database. $ink is optional and if that is omitted, then the identifier returned from the last connection to
server will be assumed as link.
Code 3: dbselect.php
<html>
<head>
<title>Server test Page </title>
</head>
<body>
<p>Sele#ting MySQL /atabase.</p>
<php
$link % mys&l'#onne#t()mys&l'host)* )mys&l'+ser)* )mys&l'password), or
die()"o+ld not #onne#t - ) . mys&l'error(,,.
print )"onne#ted s+##essf+lly<p>).
$/1 % )+ser).
mys&l'sele#t'db($/1, or die ()/atabase $/1 not sele#t..) .
mys&l'error(,,.
print )S+##essf+lly sele#t the /atabase- $/1 ).
>
</body>
</html>
'he dbselect'(h( produces output as shown in )%t(%t"*.
Output 3 :- dbselect.php
Sele#ting MySQL /atabase.
"onne#ted s+##essf+lly
S+##essf+lly sele#t the /atabase- +ser
Adding data to table
'o add data to table, we need to build and e.ecute a "#$ )uery. PHP provides the mysql_q%ery!
function for that purpose. mys)l4)uery/2 re)uires two inputs& first, the "#$ )uery and second, a link
identifier. dentifer is optional. f omited, then )uery is sent to the database server to which you last
connected.
mys)l4)uery/2 returns tr%e if the )uery e.ecutes successfully. f there are any synta. errors or if you do
not have permission to access database, then it return +alse.
Code 4 :- adddata.php
<html>
<head>
<title>Server test Page </title>
</head>
<body>
<p>/isplay data from MySQL /atabase.</p>
<php
$link % mys&l'#onne#t()mys&l'host)* )mys&l'+ser)* )mys&l'password), or
die()"o+ld not #onne#t - ) . mys&l'error(,,.
print )"onne#ted s+##essf+lly<p>).
$/1 % )+ser).
$table % )visitor).
mys&l'sele#t'db($/1, or die ()/atabase $/1 not sele#t..) .
mys&l'error(,,.
print )S+##essf+lly sele#t the /atabase- $/1 ).
$&+ery % )89S32T 89T5 $table(name*address*email,
val+es(>ma?>*>nagp+r>*>#dy7k7==7@yahoo.#o.in>,).
if ( A mys&l'&+ery( $&+ery* $link, ,
die ( )MySQL error.....<p>) .mys&l'error(, ,.
print )<p>S+##essf+lly data added to table - $table).
>
</body>
</html>
'he adddata'(h( produces output as shown in )%t(%t",.
Output 4: adddata.php
/isplay data from MySQL /atabase.
"onne#ted s+##essf+lly
S+##essf+lly sele#t the /atabase- +ser
S+##essf+lly data added to table - visitor
-etrieve data
'he mys)l4)uery/2 function of PHP also allow us to get data from table. 'he following code gives you an
idea about that...
$res+lt % mys&l'&+ery( )S3L3"T ! B25M
$table),.
$total'rows % mys&l'n+m'rows( $res+lt ,.
After perfoming "5$5C' operation using mys)l4)uery/2 function, result is stored in the identifier .res%lt.
3ow with the mysql_n%m_ro$s! function, we get the total number of rows of table *visitor+.
3ow, to display data of table, we use the PHP function mysql_+etch_ro$! which gives us all data of the
table. mys)l4fetch4row/2 returns +alse when it finds end-of-data in the table. %e use it with the while
condition to display the content of table *visitor+. 6ollowing code displays table content /Code-72.
Code 5: printdata.php
<html>
<head>
<title>Server test Page </title>
</head>
<body>
<p>/isplay table #ontent of MySQL /atabase.
<php
$link % mys&l'#onne#t()mys&l'host)* )mys&l'+ser)* )mys&l'password), or
die()"o+ld not #onne#t - ) . mys&l'error(,,.
print )<p>"onne#ted s+##essf+lly<p>).
$/1 % )+ser).
$table % )visitor).
mys&l'sele#t'db($/1, or die ()/atabase $/1 not sele#t..) .
mys&l'error(,,.
print )S+##essf+lly sele#t the /atabase- $/1 ).
$res+lt % mys&l'&+ery( )S3L3"T ! B25M $table),.
$total'rows % mys&l'n+m'rows( $res+lt ,.
print )<p>There are $total'rows in table - $table </p><p> Table #ontents
are- </p><P>).
print )<table border%C> Dn).
while ( $pr'row % mys&l'fet#h'row( $res+lt , ,
E
print )<tr>).
forea#h ( $pr'row as $data ,
print )Dt <td>$data</td>).
print )</td>Dn).
F
print )</table>Dn).
mys&l'#lose ( $link ,.
>
</body>
</html>
After e.ecuting Code"/ at my terminal, got output as in 0ig%re"#. /Please note that have added data to
the table. Here you see only the structure of the output.2
0ig%re #
1rror Handling
%hen we are performing any operation on !y"#$ using PHP, if any error occurs, our script will not work
properly. A single error can cause hundreds of lines of code to not work properly. Here, PHP provides
some special functions to print more informative error messages to the browser to aid debugging. !y"#$
gives an error message and an error number when an operation fails. PHP provides the function
mysql_error! to print error message and mysql_errno! to print error number to browser, which
becomes very useful while debugging the code. 8ou can easily get output for mys)l4error/2 if any error
occurs while e.ecuting code given in this article. 8ou can 9ust replace mys)l4error/2 with mys)l4errno/2 to
get an error number in place of error message.
'he author can be reached at cdy:k:;;:<yahoo.co.in