I have got data from mysql_fetch_assoc stored in to an array using this command
if(mysql_num_rows($data) > 1){
while($row = mysql_fetch_assoc($data)){
$ndata[] = $row;
}
} else {
$ndata = mysql_fetch_assoc($data);
}
Now when I use count on $ndata, it retuns 1; although it is empty.
When I run mysql_num_rows on the returned data it retuns 0 rows. But when I convert the data to $ndata and then run count on that it returns 1. I want it to return the number of rows.
Thanks
Can someone please explain why is there a problem and how to fix it?
Your logic is wrong: you don’t test for the case that
mysql_num_rows($data) == 0. If this is the case, your code executes the same path as when the number of rows is 1 ($ndata = mysql_fetch_assoc($data);). But there are no more rows to return (there are no rows at all), somysql_fetch_assocreturnsfalse. Andcount(false)returns1, because that’s howcountworks.Do it this way:
You can either return
nullorarray()in the firstifbranch; these are the only two values for whichcountreturns 0.