mat3000000 Posted November 1, 2010 Share Posted November 1, 2010 Get an error saying: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\admin\delete.php on line 16 Sorry, but we can not find an entry to match your query Surely this means the code has worked because of the last line, but i can't get rid of the error! $q = "DELETE FROM `stocklist` WHERE `Stock Number`='".$rec."'"; $res = mysql_query($q, $link) or die (mysql_error()); $anymatches = mysql_num_rows($res); if ($anymatches == 0) die ('Sorry, but we can not find an entry to match your query<br><br>'); echo '<h1>Entry has been deleted!</h1><br><br>'; mysql_close($link); Quote Link to comment https://forums.phpfreaks.com/topic/217480-mysql_num_rows-error/ Share on other sites More sharing options...
gizmola Posted November 1, 2010 Share Posted November 1, 2010 mysql_num_rows only works for SELECT statements. You need to check: http://www.php.net/manual/en/function.mysql-affected-rows.php Quote Link to comment https://forums.phpfreaks.com/topic/217480-mysql_num_rows-error/#findComment-1129105 Share on other sites More sharing options...
mat3000000 Posted November 1, 2010 Author Share Posted November 1, 2010 OK, here is what I have now but the output is "Query Failure" Why??? $q = "DELETE FROM `stocklist` WHERE `Stock Number`='".$rec."'"; $m = mysql_affected_rows() or die (mysql_error()); //if not numerical must use '".$eg."' $res = mysql_query($q, $link) or die (mysql_error()); if ($m < 0) echo ('Query Failure'); if ($m == 0) echo ('Sorry, but we can not find an entry to match your query<br><br>'); if ($m > 0) echo '<h1>Entry has been deleted!</h1><br><br>'; mysql_close($link); ?> Quote Link to comment https://forums.phpfreaks.com/topic/217480-mysql_num_rows-error/#findComment-1129112 Share on other sites More sharing options...
AbraCadaver Posted November 1, 2010 Share Posted November 1, 2010 You probably want to use mysql_affected_rows() AFTER mysql_query() Quote Link to comment https://forums.phpfreaks.com/topic/217480-mysql_num_rows-error/#findComment-1129113 Share on other sites More sharing options...
mat3000000 Posted November 1, 2010 Author Share Posted November 1, 2010 Now there is no output at all???? $q = "DELETE FROM `stocklist` WHERE `Stock Number`='".$rec."'"; $res = mysql_query($q, $link); $m = mysql_affected_rows() or die (mysql_error()); if ($m < 0) echo ('Query Failure'); if ($m == 0) echo ('Sorry, but we can not find an entry to match your query<br><br>'); if ($m > 0) echo '<h1>Entry has been deleted!</h1><br><br>'; mysql_close($link); Quote Link to comment https://forums.phpfreaks.com/topic/217480-mysql_num_rows-error/#findComment-1129114 Share on other sites More sharing options...
AbraCadaver Posted November 1, 2010 Share Posted November 1, 2010 $q = "DELETE FROM `stocklist` WHERE `Stock Number`='".$rec."'"; $res = mysql_query($q, $link) or die(mysql_error()); $m = mysql_affected_rows($res); Quote Link to comment https://forums.phpfreaks.com/topic/217480-mysql_num_rows-error/#findComment-1129116 Share on other sites More sharing options...
mat3000000 Posted November 1, 2010 Author Share Posted November 1, 2010 I tried that before but it gave me: Warning: mysql_affected_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\admin\delete.php on line 19 ?????? Quote Link to comment https://forums.phpfreaks.com/topic/217480-mysql_num_rows-error/#findComment-1129117 Share on other sites More sharing options...
AbraCadaver Posted November 1, 2010 Share Posted November 1, 2010 I tried that before but it gave me: Warning: mysql_affected_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\admin\delete.php on line 19 ?????? You tried this??? $res = mysql_query($q, $link) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/217480-mysql_num_rows-error/#findComment-1129118 Share on other sites More sharing options...
mikosiko Posted November 1, 2010 Share Posted November 1, 2010 @shawn $m = mysql_affected_rows($res); int mysql_affected_rows ([ resource $link_identifier ] ) Quote Link to comment https://forums.phpfreaks.com/topic/217480-mysql_num_rows-error/#findComment-1129119 Share on other sites More sharing options...
gizmola Posted November 1, 2010 Share Posted November 1, 2010 Your original code was better structured. mysql_affected_rows() isn't a catchall error function, it will work when you actually execute a valid query. Your original code checked that, but you've restructured things now, so that it's hard to tell what is going on. How about going back to: $q = "DELETE FROM `stocklist` WHERE `Stock Number`='".$rec."'"; $res = mysql_query($q, $link) or die (mysql_error()); $anymatches = mysql_affected_rows($res); if ($anymatches == 0) die ('Sorry, but we can not find an entry to match your query '); echo 'Entry has been deleted! '; mysql_close($link); In general, this points out a good practice to get into. -When you're trying to get something to work, don't refactor at the same time. Get the code working, THEN refactor. You are busy attempting to do both things at the same time, which leads to confusion. Quote Link to comment https://forums.phpfreaks.com/topic/217480-mysql_num_rows-error/#findComment-1129121 Share on other sites More sharing options...
AbraCadaver Posted November 1, 2010 Share Posted November 1, 2010 @shawn $m = mysql_affected_rows($res); int mysql_affected_rows ([ resource $link_identifier ] ) Doh!!! $q = "DELETE FROM `stocklist` WHERE `Stock Number`='".$rec."'"; $res = mysql_query($q, $link) or die(mysql_error()); $m = mysql_affected_rows($link) ; Quote Link to comment https://forums.phpfreaks.com/topic/217480-mysql_num_rows-error/#findComment-1129122 Share on other sites More sharing options...
mat3000000 Posted November 1, 2010 Author Share Posted November 1, 2010 OMG! it was so obvius... Sorry for wasting your time, Thanks a load for your help though! Quote Link to comment https://forums.phpfreaks.com/topic/217480-mysql_num_rows-error/#findComment-1129123 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.