-4

I do not understand why this is happening, please help me! Thanks.

Here is my code:

$sql1 = mysql_query("SELECT * FROM subcategory WHERE category_id = $currentCategory ORDER BY display_name ASC");
$subCatHtml = '<li class="'.($currentSubCategory == '' ? 'active' : '').'"><a href="?c='.$currentCategory.'">All</a></li>';
$scCount = mysql_num_rows($sql1); // count the output amount
while ($row = mysql_fetch_array($sql1)) {
    $subCatHtml .= '<li class="'.($row["id"]==$currentSubCategory ? 'active' : '').'"><a href="?c='.$currentCategory.'&s='.$row["id"].'">'.$row["display_name"].'</a></li>';
}

Here is the error:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/content/21/11311721/html/product_display_inlcude.php on line 17

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/content/21/11311721/html/product_display_inlcude.php on line 18

halfer
  • 19,471
  • 17
  • 87
  • 173
George
  • 1

3 Answers3

0

You have an error in your sql-syntax so mysql_query returns false.

Try this:

$sql1 = mysql_query("SELECT * FROM subcategory WHERE category_id = $currentCategory ORDER BY display_name ASC") or die(mysql_error());

And check which error you have.

Watch out:

Please do not use the mysql_* functions anymore and switch to the mysqli_* functions. Furthermore you should escape your values before inserting them into a sql-query.

Tobias Golbs
  • 4,506
  • 3
  • 26
  • 49
  • Uh, and why are you posting mysql_query() answers then, if you feel they shouldn't be used due to deprecation / future removal? – mario Jul 28 '13 at 13:03
  • @mario: Because at the end it is his decision. – Tobias Golbs Jul 28 '13 at 13:11
  • thank you, this is the response that i got: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY display_name ASC' at line 1. what do i do know? – George Jul 28 '13 at 17:57
  • @George: Does your table `subcategory` has the column `display_name` and it is written exactly like this? – Tobias Golbs Jul 28 '13 at 18:38
  • yes the table subcategory has a column display_name and it is written like this – George Jul 28 '13 at 18:54
  • Ok well. So do you even have the variable `$currentCategory` or did you misplaced it with `$currentSubCategory`? – Tobias Golbs Jul 28 '13 at 19:49
  • yes i do have both variables as i have a menu system that i am running that uses them, i dont understand why it is giving me an error as the syntax that i am using is correct? – George Jul 28 '13 at 20:01
  • Did you tried to echo the query and doublecheck it? – Tobias Golbs Jul 28 '13 at 20:01
  • if i echo $sql1 it gives me this error: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/content/21/11311721/html/product_display_inlcude.php on line 17 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/content/21/11311721/html/product_display_inlcude.php on line 18 – George Jul 28 '13 at 20:08
  • $sql1 = mysql_query("SELECT * FROM subcategory WHERE category_id = $currentCategory ORDER BY display_name ASC"); $subCatHtml = '
  • All
  • '; $scCount = mysql_num_rows($sql1); // count the output amount while ($row = mysql_fetch_array($sql1)) { $subCatHtml .= '
  • '.$row["display_name"].'
  • '; } – George Jul 28 '13 at 20:08
  • i think i am going to try another approach to what i want the page to display, thanks for the help :) – George Jul 28 '13 at 20:14
  • I meant you should echo the string... Well then good luck with that. – Tobias Golbs Jul 28 '13 at 20:15