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 :ehh: 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) ??????????????
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.
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.
Re: What does mysql_num_rows return if no rows in the select set
PHP Code:
if (is_resource($checkgroup))
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.
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.