mat3000000 Posted October 27, 2010 Share Posted October 27, 2010 The 2 errors I am getting are: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\searchstock2.php on line 36 Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\searchstock2.php on line 38 I am trying to search a table and return results, all fields are VARCHAR except ID (integer), here is part of my code; $link = mysql_connect("localhxxxxx","xxx",""); //(host, username, password) mysql_select_db("wadkin", $link) or die("Unable to select database"); //select which database we're using // Build SQL Query $query = "select * from stocklist where Stock Number like \'%$trimmed%\'OR Name like \'%$trimmed%\' OR Category like \'%$trimmed%\'"; if ($numresults=mysql_query($query)); $row = mysql_fetch_assoc($numresults); if ($row['COUNT(*)'] == 0); $numrows=mysql_num_rows($numresults); if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; } // Determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results"; $count = 1 + $s ; // display the results returned while ($row= mysql_fetch_array($result)) { $title = $row["Name"]; echo "$count.) $title" ; $count++ ; } $row = mysql_fetch_assoc($numresults); = line 36 $numrows=mysql_num_rows($numresults); = line 38 Quote Link to comment https://forums.phpfreaks.com/topic/216974-help-mysql_num_rows-expects-parameter-1-to-be-resource-boolean-given/ Share on other sites More sharing options...
trq Posted October 27, 2010 Share Posted October 27, 2010 mysql_fetch_assoc expects a valid result resource, you need to check that $numresults is not falase (which is a bool) before trying to use it. Quote Link to comment https://forums.phpfreaks.com/topic/216974-help-mysql_num_rows-expects-parameter-1-to-be-resource-boolean-given/#findComment-1127042 Share on other sites More sharing options...
mat3000000 Posted October 27, 2010 Author Share Posted October 27, 2010 Can you tell me how to do this in the code please, Thanks Or even in the table? Quote Link to comment https://forums.phpfreaks.com/topic/216974-help-mysql_num_rows-expects-parameter-1-to-be-resource-boolean-given/#findComment-1127199 Share on other sites More sharing options...
trq Posted October 27, 2010 Share Posted October 27, 2010 Actually, having another look at your code, it looks like you very much attempted to do so, you just have syntax errors. if ($numresults=mysql_query($query)) { if (mysql_num_rows($numresults)) { // successfullu use $numresults } else { // no results found } } else { // query failed } Quote Link to comment https://forums.phpfreaks.com/topic/216974-help-mysql_num_rows-expects-parameter-1-to-be-resource-boolean-given/#findComment-1127222 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.