Unit4 Phpdatabase
Unit4 Phpdatabase
The reason for using PHP as an interface to MySQL is to format the results of SQL queries in a
form visible in a web page. As long as you can log into your MySQL installation using your
username and password, you can also do so from PHP. instead of using MySQLs command line
to enter instructions and view output, you will create query strings that are passed to MySQL.
When MySQL returns its response, it will come as a data structure that PHP can recognize
instead of the formatted output you see. PHP commands can retrieve the data and format it for
the web page.
The Process
The process of using MySQL with PHP is:
1. Connect to MySQL.
2. Select the database to use.
3. Build a query string.
4. Perform the query.
5. Retrieve the results and output it to a web page.
6. Repeat Steps 3 to 5 until all desired data have been retrieved.
7. Disconnect from MySQL.
The following code fragment uses mysql_connect() to connect to the MySQL database server:
$link = mysql_connect( "localhost", "root", "n1ckel" );
if ( ! $link )
die( "Couldn't connect to MySQL" );
If you are using PHP in conjunction with Apache, you could also connect to the database server
with mysql_pconnect().
Selecting a Database
Now that we have established a connection to the MySQL daemon, we must choose which
database we want to work with. You can select a database with the mysql_select_db() function.
mysql_select_db() requires a database name and optionally accepts a link identifier. If you omit
this, the identifier returned from the last connection to the server will be assumed.
mysql_select_db() returns true if the database exists and you are able to access it.
mysql_select_db( $sample ) or die ( "Couldn't open $sample );
Finding Out About Errors
So far we have tested the return values of the MySQL functions that we have used and called
die() to end script execution if a problem occurs. You might, however, want to print more
informative error messages to the browser to aid debugging. MySQL sets an error number and an
error string whenever an operation fails. You can access the error number with mysql_errno(),
and the error string with mysql_error().
<html>
<body>
<?php
$user = "root";
$pass = "";
$db = "sample";
$link = mysql_connect( "localhost", $user, $pass );
if ( ! $link )
die( "Couldn't connect to MySQL" );
print "Successfully connected to server<P>";
mysql_select_db( $db )
or die ( "Couldn't open $db: ".mysql_error() );
print "Successfully selected database \"$db\"<P>";
mysql_close( $link );
?>
</body>
</html>
We can add information to one of the tables in the database. we will need to construct and
execute a SQL query. PHP provides the mysql_query() function for this purpose. mysql_query()
requires a string containing a SQL query and, optionally, a link identifier. If the identifier is
omitted, the query is sent to the database server to which you last connected. Mysql_query()
returns a positive value if the query is successful. If your query contains a syntax error, or if you
don't have permission to access the database in question, then query() returns false.
<html>
<body>
<?php
$user = "root";
$pass = "";
$db = "sample";
$link = mysql_connect( "localhost", $user, $pass );
if ( ! $link )
die( "Couldn't connect to MySQL" );
<html>
<body>
<?php
$user = "root";
$pass = "";
$db = "sample";
$link = mysql_connect( "localhost", $user, $pass );
if ( ! $link )
die( "Couldn't connect to MySQL" );
mysql_select_db( $db, $link )
or die ( "Couldn't open $db: ".mysql_error() );
$result = mysql_query( "SELECT * FROM domains" );
$num_rows = mysql_num_rows( $result );
print "There are currently $num_rows rows in the table<P>";
mysql_close( $link );
?>
</body>
</html>