Why doesn't this code from O'Reilly's
PHP and MySQL work for me?
pp. 172-173 of the book gives the following code, minus the
print "wtf #"; lines, which I added to try to help me figure out where things went wrong.
The results I'm getting--with, naturally, the database name, user account, and password changed to what they should be--are an HTML page with the total content "wtf 0". Or, in other words, all of the page having to do with connecting to the database, querying it, and printing the results, fails.
I've checked the user account name, the password, the database, and the table several times. Convinced I've missed something, I've then typed each field into another page to paste from there into both the account management screens and into the PHP, ensuring that they all match. It's still not working.
I am not getting any error messages, just that puzzling single line of output followed by nothing more.
I've tried flushing the browser cache and even testing the page in different browsers. They all show the same result.
I can't help feeling that this is probably a palm-forehead error, the kind I specialize in, but I don't know how to go about searching for likely culprits when the results are a complete failure to return any results (an error message would be a big help).
Any ideas what I've most likely done wrong?
Apache 2.2.4, PHP 5.2.2, MySQL 5.0
Code below:
<pre>
<?php
print "wtf 0";
// (1) Open the database connection and use the winestore
// database
$connection = mysql_connect("localhost","fred","shhh");
mysql_select_db("winestore", $connection);
print "wtf 1";
// (2) Run the query on the vols through the
// connection
$result = mysql_query ("SELECT * FROM
wine", $connection);
print "wtf 2";
// (3) While there are still rows in the result set,
// fetch the current row into the array $row
print "wtf 3";
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
// (4) Print out each element in $row, that is,
// print the values of the attributes
foreach ($row as $attribute)
print "{$attribute} ";
print "wtf 4";
// Print a carriage return to neaten the output
print "\n";
}
// (5) Close the database connection
mysql_close($connection);
print "wtf 5";
?>
</pre>
posted by orthogonality at 8:18 PM on June 16, 2007