|
-
May 3rd, 2010, 04:22 AM
#1
Thread Starter
Hyperactive Member
What does mysql_num_rows return if no rows in the select set
Trying to test that a record doesn't exist on a table and am unable to work out how to code around a record set with zero rows It doesn't return 0, which sucks somewhat.
Anyone have any ideas
Code:
$q = "SELECT * FROM lb_group WHERE grp_id = '".$groupid."'";
$checkgroup = mysql_query($q, $connect);
$num = mysql_num_rows($checkgroup):
if(!$num) ??????????????
-
May 3rd, 2010, 09:36 AM
#2
Re: What does mysql_num_rows return if no rows in the select set
I don't know whether to assume that you're getting a return value of -1, or false. if -1:
PHP Code:
if($num <= 0){ // error? }else{ // do something productive }
if false, you can just check if the variable evaluates to false, as you're doing (!$num). this will catch a value of false and a value of 0.
mysql_num_rows() could maybe return -1, but will only return false if the function call failed for some reason.
-
May 3rd, 2010, 05:47 PM
#3
Thread Starter
Hyperactive Member
Re: What does mysql_num_rows return if no rows in the select set
Thanks Kows,
Weirdly it returns a value if a row is found but isn't entirely communiative if one isn't.
Seems to be working with some tweaking, but need to test a few more things before signing off on it. Guess I was using another language's constructs that don't work terribly well with PHP/MySQL.
-
May 6th, 2010, 12:17 AM
#4
Re: What does mysql_num_rows return if no rows in the select set
PHP Code:
if (is_resource($checkgroup))
-
May 30th, 2010, 08:13 PM
#5
Member
Re: What does mysql_num_rows return if no rows in the select set
Try to get know even the query is done correctly or no first:
Code:
$q = "SELECT * FROM lb_group WHERE grp_id = '".$groupid."'";
$checkgroup = mysql_query($q, $connect);
if ($checkgroup == NULL)
{
//ERROR
}
else
{
$num = mysql_num_rows($checkgroup):
}
and to make sure no error shows up then try:
Code:
if( ($num == 0) OR ($num == NULL) ) {
// error?
}else{
// do something productive
}
I used this and it was working fine for me in my projects.
Last edited by Luinox86; May 30th, 2010 at 08:17 PM.
-
May 30th, 2010, 10:13 PM
#6
Re: What does mysql_num_rows return if no rows in the select set
mysql_query() and mysql_num_rows() do not return null. they either return a resource/integer (respectively) or boolean false on error.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|