PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
$sql = "SELECT sUserId, xRequest FROM hs_request WHERE sUserid != '' AND interaction_entry = FALSE";
$result = mysql_query( $sql );
$numrows=mysql_num_rows($result);
if ($numrows == 0)
{
exit("No records to update");
}
The above code just drags some records out of a table, I was hoping that if there were no records then just exit the script. Seems that when there are no records then I get 'Resource id #2'
Anyone enlighten me on this?
Oh one final thing. I do a Where sUserid != '', is this a bad thing? I am basically looking for all the fields that do have a userid entered in them. Is this the right way of checking if they are empty?
Jim
Last edited by hairyjim on Fri Jun 09, 2006 9:50 am, edited 2 times in total.
All I am doing is pulling records from the table that have a userID and have not had an interaction logged against them. The xRequest is an ID of a support incident used further down the script.
What is important here rather than what this snippet of a very large script is used for is that when there are no results returned from the SQL I get 'Resource ID # 2' in the array.
Thus the check on the number of rows returned is not executed since it returns a row containing 'Resource ID # 2', even though there are no records matching the SQL criteria.
My test data was setup so there were no results to return.
I expected no results from the SQL query, thus $result to have no rows in it. Therefore I also expected the if statement to be executed since I also expected $numrows to be 0 - i.e there were no rows returned.
Instead I got the 'Resource ID # 2' returned in the result which set $numrows to 1.
hairyjim wrote:Am I just completly misisng something here?
Looks like it. If it was a SELECT query $result will either contain bool(false) (this means the query failed, possibly due to a parse error or non-existant element) or it will contain a resource id that will be used by php to interact with mysql when using any other function with regard to that query.
$sql = "SELECT sUserId, xRequest FROM hs_request WHERE sUserid != '' AND interaction_entry = FALSE";
$result = mysql_query( $sql );
$numrows=mysql_num_rows($result);
So in the above I expect $numrows to be 0 because I set the data up so there would be no results to return. Therefore I expected the below code to execute because $numrows = 0.
if ($numrows == 0)
{
exit("No records to update");
}
BUT from what I understand is that you are telling me is that on a succesful query a resource ID row is also returned regardless if there is actually any table data to return. So my if statement would not execute.
As long as the query was good (i.e. no parse errors, the table and columns exist and the grammer is correct) if the query is a SELECT query it will return a resourse id.
You understood only part of it. Here are the two possible scenarios:
1. $result holds boolean false
2. $result holds resource id
If everything is ok, then the second scenario should take place. Thus, when calling to mysql_num_rows($result); (and assuming the table is empty as you said) - $numrows would hold '0', and in this case the code inside the if () statement should be executed.
When I now echo out the num_rows I get 0. Which is correct.
Strange the behaviour I was bleating on about seems ot have vanished, perhaps I had an error elsewhere before this code that caused the stranged behaviour.
Thanks for all the suggestions, you all have indeed enlightened me.