Hello, I'm sure I'm missing something small. If a category doesn't exist in the database yet, how can I have the query accept a value of 0 without reporting an error? The $subcategory is what the user selected. The result should either be 4 images or a blank place holder with an echo (couldn't figure out the echo either, just got more errors.)

Here is my error Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /product.php on line 38

Thank you for your help.

include "storescripts/connect_to_mysql.php"; 
$tinysubcategory = "";
$tinysql = mysql_query("SELECT * FROM products WHERE subcategory ='". $subcategory ."' LIMIT 4");
$tinyproductCount = mysql_num_rows($tinysql); // count the output amount
if ($tinyproductCount > 0) {
    while($row = mysql_fetch_array($tinysql)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $details = $row["details"];
             $subcategory = $row["subcategory"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
             $tinysubcategory .= 
            <th valign="top">
             <a href="product.php?id=' . $id . '">
             <img style="border:#FFF 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="70" height="70" border="2px" />
            <h3> <a href="product.php?id= ' . $id . '"> ' . $product_name . ' </a> </h3> 
} else {
    $tinysubcategory .= 
    '<img style="border:#FFF 1px solid;" src="inventory_images/productholder.png" alt="Nothing to display!" width="70" height="70"" />

mysql_close(); ?>

<? echo $tinysubcategory; ?>

Edited by cheelo007: added more code

4 Years
Discussion Span
Last Post by cheelo007

The mysql_num_rows if failing because your query is failing not because it returned 0 results. Add a die statement to your query to output the error to see what's happening:

$tinysql = mysql_query("SELECT * FROM products WHERE subcategory ='". $subcategory ."' LIMIT 4") or die (mysql_error());

On a side note you're not using your tinyProductCount variable anywhere so you're wasting resources by storing it in a variable. You can combine the two lines into one

$tinysql = mysql_query("SELECT * FROM products WHERE subcategory ='". $subcategory ."' LIMIT 4");

if (mysql_num_rows($tinysql) > 0) {

On another side note mysql functions are being depreciated, you should use mysqli or PDO


As GliderPilot says - however, just to clarify as well, the error you are getting is telling you that $subcategory (in your query) is either empty or in an incorrect format.

Where is the value assigend to $subcategory coming from (I don't see anything in your code where a value is assigned to this variable)? I am presuming you are posting the value from a form therefore you need to be using $_POST to obtain the data (for example - please adjust to match your form / variable name):

if (isset($_POST) && !empty($_POST['subcategory']) {
  $subcategory = $_POST['subcategory'];

  // Do rest of query here


Thank you! I had to contact the company that hosts my domain to get them to enable errors, it pointed me straight to where the problem was. Thank you all so much.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.