Jump to content

mysql_num_rows(): supplied argument is not a valid MySQL result resource


Recommended Posts

Hi guys,

 

I have this code here working which will show certain data in the table depending on what string the user searches for, and with the records that pop up you are able to delete these records too.. now all this is working but after I delete records I get this error:

 

"Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/define/public_html/golf/Pete/php/searchcallback.php on line 22"

 

I assume this is because num rows are empty after the deletion is done/ and this now shows "no records showing" can anyone help me why this error is popping up? Although deletion does work, I would like to get rid of this error message.

 


<?php
include "connect.php";

$searchvalue = $_POST['searchvalue'];
$su = $_POST['search'];

//Check if records were submitted for deletion
if(isset($_POST['callbackid']) && count($_POST['callbackid']))
{
    //Ensure values are ints
    $deleteIDs = implode(', ', array_map('intval', $_POST['callbackid']));
    $query = "DELETE FROM callback WHERE callbackid IN ({$deleteIDs})";
    //Debug line
    echo "<br/>Successfuly deleted callback(s) <br/>";
    mysql_query($query) or die(mysql_error());
}




$result = mysql_query("SELECT * FROM callback WHERE $searchvalue LIKE '%$su%'");
$num_rows = mysql_num_rows($result);



?>
  	<link rel="stylesheet" type="text/css" href="view.css" media="all">
<script type="text/javascript" src="view.js"></script>
<body id="main_body" >

<img id="top" src="top.png" alt="">
<div id="form_container">

<h1>Show, Search or Delete Callback Records<</h1>

	<form id="form_362567" class="appnitro"  method="post" action="searchcallback.php">
				<div class="form_description">
		<h2> Show, Search or Delete Callback Records</h2>
		<p></p>
	</div>						
		<ul >

				<li class="section_break">
<?php
if ($num_rows==0){
	?>

<br>No records found<br><br>
<INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1);return true;"><br>
<?php
} else{
while($row = mysql_fetch_array($result))
{

//..results as in your post.

echo "<form action='' method='POST'>";  
echo "<br/>";
echo "<b>Callback ID:</b>";
echo "<br/>";
echo $row['callbackid'];
echo "<br/>";
echo "<b>Full Name:</b>";
echo "<br/>";
echo $row['fullname'];
echo "<br/>";
echo "<b>E-mail:</b>";
echo "<br/>";
echo $row['email'];
echo "<br/>";
echo "<b>Phone Number:</b>";
echo "<br/>";
echo $row['phone'];
echo "<br/>";
echo "<b>Comment:</b>";
echo "<br/>";
echo $row['comment'];
echo "<br/>";
echo "<input type='checkbox' name='callbackid[]' value='{$row['callbackid']}' /> <b>Delete</b>\n";
?>
<li class="section_break">

<?php
}
?>
<br><input type='submit' value='Delete Records' name='delete' /><INPUT TYPE="button" VALUE="Cancel" onClick="history.go(-1);return true;"><br>
</form>
		<div id="footer">

	</div>
</div>
<img id="bottom" src="bottom.png" alt="">
</body>
<?php

}
?>

 

Thanks for any help in advance :)

The issue is because your not checking to see if your query succeeded before blindly passing it's result to mysql_num_rows().

 

mysql_query() will return a bool false on failure, mysql_num_rows() expects a result resource, not a bool.

Ah that makes sense, because it does not show the records afterwards as its lost the previous form, so it fails on that.. how could I retrieve this info or go around this? I might have an idea.. but any input on while I work on it is very welcome

You can't "work around" it, you need to fix your query so that it doesn't fail.

 

You also need to check your query succeeds before using it's result in any of the other mysql_* functions. eg;

 

if ($result = mysql_query("SELECT * FROM callback WHERE $searchvalue LIKE '%$su%'")) {
    if (mysql_num_rows($result)) {
        // $result is a result resource containing data, process it.
    } else {
        // no records found
    }
} else {
    // query failed, check mysql_error() to see why
}

I solved it with an else if, I am simply just not going to run that query unless I need to, all working nicely without errors now,  but thanks for your suggestion, it will help me for the future for sending out queries :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.