Database Reference
In-Depth Information
print “<TABLE border=1>
<TR><TD>WebPageID</TD><TD>Browser</TD><TD>IPNumber</TD></TR>”;
do {
$row = mysql_fetch_row($myResults);
print “<TR><TD>$row[0]</TD><TD>$row[1]</TD><TD>$row[2]</TD></TR>”;
} while ($row);
print “</TABLE>”;
Clearing the Record Set
After we have finished with the record set we need to do some housekeeping. The record set
may contain hundreds of rows if accessing a large database, which may hang around tying
up system resources if you leave them there. This could have major implications if lots of
people were looking at the webpage at one time. When you have finished processing your
record set you can use mysql_free_result as follows:
mysql_free_result($resultsSet);
Closing the Connection
Finally, we have to close the connection to the database. Although this happens automati-
cally once the script finishes processing, closing it earlier will free the resources that it holds
for use by other processes. To close the connection use:
mysql_close($myConnection);
Testing the Script
We now have all that we need to connect to the database, execute the query and process the
results. Here are all of the parts put together:
<HTML>
<TITLE>Connecting PHP to MySQL</TITLE>
<BODY>
<?PHP
$myConnection = mysql_connect(“localhost”, “phpuser”, “abominable”)
or die(“Could not connect”);
mysql_select_db(“mysqlfast”) or die(“Could not select database”);
$query = “SELECT webpageid,browser,ipnumber FROM Log”;
$myResults=mysql_query(“$query”)or die(“Invalid query: “ .
mysql_error());
print “<TABLE border=1>
<TR><TD>WebPageID</TD><TD>Browser</TD><TD>IPNumber</TD></TR>”;
Search WWH ::




Custom Search