Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PhP/MySQL Script Failure 1

Status
Not open for further replies.

tbohon

Programmer
Apr 20, 2000
293
US
As I don't know PHP very well, I've been working through the text "Practical PHP and MySQL: Building 8 Dynamic Web Applications" this weekend. Everything was going well until I began working on the shopping cart application.

Here is the code (which matches the book character-for-character) and the error I receive when loading the page:

Code:
<h1>Product Categories</h1>
<ul>
<?php

    $catsql = "SELECT * FROM categories;";
    $catres = mysql_query($catsql);
   
    while($catrow = mysql_fetch_assoc($catres))
    {
        echo "<li><a href='" . $config_basedir . "/products.php?id=" . $catrow['id'] . "'>" . $catrow['name'] . "</a></li>";
    }

?>
</ul>

The error message it gives me is

Code:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\[URL unfurl="true"]www\shoppingcart\bar.php[/URL] on line 8

I've checked two other sites in the text and, while using slightly different names, the logic for the comparable routine is exactly the same as this one ... so I'm out of ideas.

Any thoughts?

Tnx in advance.

Tom

"My mind is like a steel whatchamacallit ...
 
Note that I've cross-posted this to the MySQL forum as I'm not certain where the problem lies ... MySQL or PhP.

Tnx again.

Tom

"My mind is like a steel whatchamacallit ...
 
You might want to do some error checking before doing the fetch:
Code:
if (!$catres ) {
    echo "Could not successfully run query from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($catres ) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

Here's the reference:
 
Miros:

Error checks you suggested showed no database opened. Eventually traced it back to my config file - one silly little character off in the database name ... obviously the "do what I meant not what I said" flag hasn't been turned on here ... [wink]

On the positive side, the debugging was invaluable in helping me see and understand how mysql and php are working together here so the time wasn't wasted. Besides, my stress level was far too low after 2 days off ... need to get ready for work in the morning anyway!

Thanks a bunch for pointing me in the right direction.

Best,

Tom

"My mind is like a steel whatchamacallit ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top