1.6 Accessing MySQL from PHP

PHP for NetWare can access the MySQL database residing on the same NetWare server or on any other remote server through the MySQL extension.

Complete the following tasks, before accessing MySQL from PHP:

1.6.1 Executing a MySQL Database Sample

Execute the following sample (mysql.php) tests the database connectivity.This sample reads and displays the data from the table emp existing in the database test.

The sample is copied along with the PHP binaries in the sys:\php\webdemo directory. To use it, you will need to copy it to sys:\apache\htdocs directory or a subdirectory under htdocs directory.

Then, enter the URL, of the form http://Server_name:port_number/Destinationfolder/scriptname

For example, the URL for executing the script mysql.php located under sys:\apache\htdocs\phpscripts will be

http://server_name:port_number/phpscripts/mysql.php.

<html>

<head>
<title>MySQL Database Entries</title>
</head>

<body>

<h2>MySQL Database Entries</h2>

<?php
 $mysql_server = "localhost";

 $mysql_user_name = "UserName";
 $mysql_user_pass = "Password";
 $mysql_dbname = "Database Name";
 $mysql_table = "Table Name";

 echo "<br><b>Server Name:&nbsp;&nbsp;$mysql_server</b><br>";
 echo "<b>Database Name:&nbsp;&nbsp;$mysql_dbname</b><br>";
 echo "<b>Table Name:&nbsp;&nbsp;$mysql_table</b><br><br><br>";

 echo "<B>MySQL Query Results:</B><br><br>";

 // Connecting to MySQL database
 $link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or
die("<font color=red><b>Could not connect to MySQL database</b></font>");

 // To read from the database, select it
 mysql_select_db("$mysql_dbname") or die("<font color=red><b>Could not select
database</b></font>");

 // Perform SQL query
 $query = "SELECT * FROM $mysql_table";
 $result = mysql_query($query) or die("<font color=red><b>Query failed</b><
font>");

 $mysql_number_fields = mysql_num_fields($result);
 print "<table border><TD>";
 print "<tr><B>";
 for ($i=0; $i<$mysql_number_fields; $i++) {
     $mysql_field_name = mysql_field_name($result, $i);
     print "<th>$mysql_field_name</th>";
 }
 print "</B></tr>";

 // Printing results in HTML
 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
     print "<tr>";
     foreach ($line as $col_value) {
         print "<td>$col_value</td>";
     }
     print "</tr>";
 }
 print "</table>";

 // Free resultset
 mysql_free_result($result);

 // Closing connection
 mysql_close($link);
?>


</body>
</html>