name this bug
June 16, 2007 8:09 PM   Subscribe

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:
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

print "wtf 5";

posted by Tuwa to Computers & Internet (16 answers total) 1 user marked this as a favorite
Verify you can connect to the database and preform the queries using the mysql comannd line tool.
posted by orthogonality at 8:18 PM on June 16, 2007

mysql -u fred -pshhh winestore

> select * from wine;
posted by orthogonality at 8:19 PM on June 16, 2007

Response by poster: Jebus. The results of the SELECT * query are a new cursor ( > )

Yet in SQLYog it shows the table contains data.
posted by Tuwa at 8:23 PM on June 16, 2007

You'll want error messages to diagnose what's going on. See here for example code (see example 1372).
posted by zamboni at 8:25 PM on June 16, 2007

Response by poster: Nope, my mistake. Forgot the semicolon (repeatedly!). It pulls the data correctly.
posted by Tuwa at 8:25 PM on June 16, 2007

Check the return values of your database connection functions before proceeding. The example code doesn't do any defensive programming, and assumes everything works. Look through these pages:

posted by meowzilla at 8:31 PM on June 16, 2007

Response by poster: Hm ... trying just

// we connect to localhost
$link = mysql_connect('localhost', '$user', '$pwd');
if (!$link) {
die('Could not connect: ' . mysql_error());
echo 'Connected successfully';

returns a completely blank page (not even an error message).

You know, I bought this O'Reilly book expecting a step-by-step walkthrough....

That said, maybe this means something: that completely blank page doesn't print the /pre tag or the /body or /html. Any idea what that's all about? The <?php bit is (correctly) paired with a ?>
posted by Tuwa at 8:41 PM on June 16, 2007

Best answer: Go into your /etc/php.ini file and enable error reporting. That will give you some idea of what's going on. Also look at your /var/log/system.log files.
posted by Blazecock Pileon at 8:48 PM on June 16, 2007

A blank page generally means a parse error, which is much easier to track down with error reporting on, as Blazecock Pileon said. But if you don't have that option, you can always just remove code bit by bit until you find the invalid bit. But PHP errors are very helpful to the learning experience, so I highly recommend turning them on if you can. Going back to your original problem, if you're actually using syntax like '$user', it's probaby not connecting at all, because variables are not parsed within single quotes, only double quotes.
posted by scottreynen at 9:15 PM on June 16, 2007

Response by poster: Fatal error: Call to undefined function mysql_connect()

I suspect that this page will most likely help me solve it--something about PHP on Windows not having MySQL on by default.... Off to reboot.
posted by Tuwa at 9:32 PM on June 16, 2007

Response by poster: It's still giving the same error, but at least there's an error to work with.

Thanks for the tips, everyone.
posted by Tuwa at 9:51 PM on June 16, 2007

Fatal error: Call to undefined function mysql_connect()

Look for in that php.ini file. Uncomment the line and restart your Apache service. (This assumes that your PHP has its mysql module compiled in.)
posted by Blazecock Pileon at 10:55 PM on June 16, 2007

Best answer: As you deduce, you're very likely missing the PHP module. If you want to save headaches like this, you could use XAMPP - it's a simple install and comes preconfigured with Apache, MySQL, PHP 5, and all the modules and setups you need.
posted by pocams at 11:23 PM on June 16, 2007

Response by poster: does not exist in the php.ini file. I thought I'd done everything as recommended but obviously I missed something.

I'm going to try to figure out how to compile the mysql module; if I can't get it to go anywhere then I'll uninstall what I've got and try XAMPP instead.
posted by Tuwa at 6:03 AM on June 17, 2007

Response by poster: Now PHP quit giving the error but Apache barfs when I try to restart it, complaining about a newly missing LIBEAY32.dll.

Blargh. XAMPP it is.
posted by Tuwa at 6:20 AM on June 17, 2007

Response by poster: Woo hoo! It works! I pulled actual data from my actual database.


Thanks again everyone.
posted by Tuwa at 6:50 AM on June 17, 2007

« Older Want to add real mixing to my mix CDs on my Mac.   |   Have a Nice Decade? Newer »
This thread is closed to new comments.