Hello,

I have a little problem which I think is because of the php aspect of my database...

My code:

<?

// connect to MySQL server and select database
@mysql_connect("localhost", "--", "--")
or die("Could not connect to MySQL server!");
@mysql_select_db("basichy_ict") or die("Could not select company database!");
$keyword = $_POST["keyword"];
// form and execute query statement
$query = "SELECT id, title, author, price, genre, stock
FROM books WHERE $title = '$keyword'";
$result = mysql_query($query);// If no matches found, display message and redisplay form
if (mysql_num_rows($result) == 0) :
print "Sorry, but no matches were found. Please try your search again:";
// matches found, therefore format and display results
else :
// format and display returned row values.
list($id, $title, $author, $price, $genre, $stock) = mysql_fetch_row($result);
print "<h3>Stock Information:</h3>";
print "<b>ID:</b> $id <br>";
print "<b>title:</b> $title <br>";
print "<b>Author:</b> <a href=\"mailto:$email\">$email</a> <br>";
print "<b>Price:</b> $price <br>";
print "<b>Genre:</b> $genre <br>";
print "<b>Stock:</b> $stock <br>";
// form and execute 'orders' query
$query = "SELECT id, title, author, price, genre, stock
FROM books WHERE id = '$id'
ORDER BY quantity DESC";
$result = mysql_query($query);
print "<table border = 1>";
print "<tr><th>ID</th><th>Title</th><th>Author</th><th>Price</th><th>Genre</th><th>Stock</th></tr>";
// format and display returned row values.
while (list($id,$title,$author,$price,$genre,$stock) = mysql_fetch_row($result)):
print "<tr>";
print "<td>$id</td><td>$title</td><td>$author</td><td>$price</td><td>$genre</td><td>$stock</td>";
print "</tr>";
endwhile;
print "</table>";
endif;

?>

The output:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/basichy/public_html/ict/search.php on line 12

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/basichy/public_html/ict/search.php on line 17
Stock Information:
ID:
title:
Author:
Price:
Genre:
Stock:

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/basichy/public_html/ict/search.php on line 33
ID	Title	Author	Price	Genre	Stock

Any ideas?

    Plenty.....
    Read my first article: PHP Helper (it's in my sig)

      Ehh

      The code is from a tutorial so I was thinking that it should be problem free 😃

        That's never true. Plenty of code in tuts has problems (including my own). But read my article, I know I talked about your error. You only have 1 error, the rest piggy-back off of the main one.

          No, the problem isn't in the mysql_num_rows... read your error. What does it tell you?

          Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/basichy/public_html/ict/search.php on line 12

          The problem is in your query. You need to add an "or die(mysql_error())" statement and see what mySQL tells you is wrong.... I guarantee you it's with your query (i.e. its' not even completing).

          $query = "SELECT id, title, author, price, genre, stock
          FROM books WHERE $title = '$keyword'";

          Where do you define $title and $keyword?

            I took out a part of the code that I didn't need, erased the $ on the title, and changed a command so I can get better results

            Now it works great

            Thanks!!

              Great!! Glad to hear it's working!!

              NOTE: Don't forget to mark this thread resolved.
              Thread Tools --> Mark Thread Resolved

                Write a Reply...