Click to See Complete Forum and Search --> : What does mysql_num_rows return if no rows in the select set
KiwiDexter
May 3rd, 2010, 04:22 AM
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
$q = "SELECT * FROM lb_group WHERE grp_id = '".$groupid."'";
$checkgroup = mysql_query($q, $connect);
$num = mysql_num_rows($checkgroup):
if(!$num) ??????????????
kows
May 3rd, 2010, 09:36 AM
I don't know whether to assume that you're getting a return value of -1, or false. if -1:
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.
KiwiDexter
May 3rd, 2010, 05:47 PM
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.
penagate
May 6th, 2010, 12:17 AM
if (is_resource($checkgroup))
Luinox86
May 30th, 2010, 08:13 PM
Try to get know even the query is done correctly or no first:
$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:
if( ($num == 0) OR ($num == NULL) ) {
// error?
}else{
// do something productive
}
I used this and it was working fine for me in my projects.
kows
May 30th, 2010, 10:13 PM
mysql_query() (http://ca3.php.net/manual/en/function.mysql-query.php) and mysql_num_rows() (http://ca3.php.net/manual/en/function.mysql-num-rows.php) do not return null. they either return a resource/integer (respectively) or boolean false on error.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.