wizzle Posted January 15, 2012 Share Posted January 15, 2012 hi! i badly need your help.. i'm having an error in my addressbook system.. Here's the error: mysql_num_rows() expects parameter 1 to be resource, null given in /Applications/XAMPP/xamppfiles/htdocs/sampol3/design/edit.php on line 74. please give the right code.. thank you.. 17348_.php Quote Link to comment https://forums.phpfreaks.com/topic/255046-help-me-please/ Share on other sites More sharing options...
trq Posted January 15, 2012 Share Posted January 15, 2012 Firstly, I haven't looked at your code because I won't download code. Anyway, your error is very common and is caused by passing the results of a call to mysql_query() straight into mysql_num_rows() without first checking to see that your query has succeeded. mysql_num_rows() expects a result resource, while mysql_query() will return a result resource on success or the boolean false on failure. For some reason, your query is failing. What does mysql_error have to say? Quote Link to comment https://forums.phpfreaks.com/topic/255046-help-me-please/#findComment-1307785 Share on other sites More sharing options...
wizzle Posted January 15, 2012 Author Share Posted January 15, 2012 here' the function for mysql_error(): mysql_error($con) $con=mysql_connect("localhost",$username,$password); if(!$con) die("couldn't connect to MySQL: ".mysql_error()); mysql_select_db($db_name,$con) or die("couldn't connect to $db_name: ".mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/255046-help-me-please/#findComment-1307796 Share on other sites More sharing options...
Porl123 Posted January 15, 2012 Share Posted January 15, 2012 Try adding or die(mysql_error()) to the end of your mysql_fetch_array() call like this: mysql_fetch_array($query) or die(mysql_error()); This will give you a more precise definition of the problem. Quote Link to comment https://forums.phpfreaks.com/topic/255046-help-me-please/#findComment-1307798 Share on other sites More sharing options...
trq Posted January 15, 2012 Share Posted January 15, 2012 Try adding or die(mysql_error()) to the end of your mysql_fetch_array() call like this: mysql_fetch_array($query) or die(mysql_error()); This will give you a more precise definition of the problem. Or better still, handle your errors properly. An example: if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { // $result is now good to use } else { // no results found } } else { trigger_error(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/255046-help-me-please/#findComment-1307800 Share on other sites More sharing options...
wizzle Posted January 15, 2012 Author Share Posted January 15, 2012 switch($select) { case 1: $result=mysql_query("SELECT * FROM tb_addressbook WHERE lastname LIKE'%$text%'"); break; case 2: $result=mysql_query("SELECT * FROM tb_addressbook WHERE firstname='$text'"); break; case 3: $result=mysql_query("SELECT * FROM tb_addressbook WHERE address='$text'"); break; } if (!$result=) { echo "Could not successfully run query ($result) from database" . mysql_error($con); } if (mysql_num_rows($result) == 0) //(im having a problem in this line. { echo "<br>No rows found, Please try again! <br> <a href='searchform.php'>search</a>"; echo "<br><br>"; exit; } Quote Link to comment https://forums.phpfreaks.com/topic/255046-help-me-please/#findComment-1307801 Share on other sites More sharing options...
Porl123 Posted January 15, 2012 Share Posted January 15, 2012 lastname LIKE'%$text%'"); Could be due to there not being a space between LIKE and ' Not sure though, just a punt. Quote Link to comment https://forums.phpfreaks.com/topic/255046-help-me-please/#findComment-1307803 Share on other sites More sharing options...
wizzle Posted January 15, 2012 Author Share Posted January 15, 2012 lastname LIKE'%$text%'"); Could be due to there not being a space between LIKE and ' Not sure though, just a punt. i already put a space., but still same error and problem occur.. :'( :'( Quote Link to comment https://forums.phpfreaks.com/topic/255046-help-me-please/#findComment-1307812 Share on other sites More sharing options...
Porl123 Posted January 15, 2012 Share Posted January 15, 2012 Could it be that $select doesn't contain 1, 2 or 3 and seeing as your switch case doesn't contain a default clause, it doesn't set a query result to $result? Quote Link to comment https://forums.phpfreaks.com/topic/255046-help-me-please/#findComment-1307814 Share on other sites More sharing options...
trq Posted January 15, 2012 Share Posted January 15, 2012 While your problem is with your query you have a logic issue here. if (!$result=) { echo "Could not successfully run query ($result) from database" . mysql_error($con); } If $result is false you echo an error message. this is good, however, you never stop your code from going into the next part: if (mysql_num_rows($result) == 0) If $result is false, you will always get an error here because (as I have already explained) mysql_num_rows expects a result resource. Nothing else can be passed to it. Quote Link to comment https://forums.phpfreaks.com/topic/255046-help-me-please/#findComment-1307816 Share on other sites More sharing options...
PFMaBiSmAd Posted January 15, 2012 Share Posted January 15, 2012 This code seems familiar. Wizzle, you already bumped an old thread with a post to add this or very similar code, where I relied what is causing the symptom and what you needed to do to troubleshoot what is causing the problem - http://www.phpfreaks.com/forums/index.php?topic=272971.msg1658897#msg1658897 Quote Link to comment https://forums.phpfreaks.com/topic/255046-help-me-please/#findComment-1307849 Share on other sites More sharing options...
wizzle Posted January 15, 2012 Author Share Posted January 15, 2012 i already fix the problem,... thank you guys.. Quote Link to comment https://forums.phpfreaks.com/topic/255046-help-me-please/#findComment-1307850 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.