开发者

php count function not showing the right count on mysql_fetch_assoc

开发者 https://www.devze.com 2023-02-21 22:56 出处:网络
I have got data from mysql_fetch_assoc stored in to an array using this command if(mysql_num_rows($data) > 1){

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开发者_Go百科 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), so mysql_fetch_assoc returns false. And count(false) returns 1, because that's how count works.

Do it this way:

$rows = mysql_num_rows($data);
if($rows == 0) {
    return null;
}
else if($rows == 1){
    $ndata = mysql_fetch_assoc($data);
} else {
    while($row = mysql_fetch_assoc($data)){
        $ndata[] = $row;
    }
}

You can either return null or array() in the first if branch; these are the only two values for which count returns 0.


I think mysql_fetch_assoc() is returning false (see doc) and count(false) returns 1.


$data = mysql_query($query);
while($row = mysql_fetch_assoc($data)){
      $ndata[] = $row;
}
if(count($ndata) == 1) $ndata = $ndata[0];

Runing query that returns 1 row and

$data = mysql_query("SELECT * FROM online LIMIT 1");
print_r($ndata);

Result

Array ( [timestamp] => 1301826108 [ip] => 213.186.122.2) 

Runing query with multiple rows...

$data = mysql_query("SELECT * FROM online");
print_r($ndata);

Result

Array
(
    [0] => Array
        (
            [timestamp] => 1301848228
            [ip] => 67.195.112.29
        )

    [1] => Array
        (
            [timestamp] => 1301826108
            [ip] => 213.186.122.2
        )

    [2] => Array
        (
            [timestamp] => 1301825465
            [ip] => 77.88.28.246
        )

    [3] => Array
        (
            [timestamp] => 1301763579
            [ip] => 69.171.224.251
        )

)


It seems I finally got what you need (However, question remains extremely vague and it's hard to tell for sure). But anyway.

$ndata = array();
while($row = mysql_fetch_assoc($data)){
  $ndata[] = $row;
}

Now you would get desired result from count($ndata). It will return 0 for no rows, 1 for 1 row and whatever number if more rows returned. Bingo!

But if you're still planning to get different kinds of arrays, I should warn you against doing it this way.
Always state explicitly what kind of result you want to get:

$ndata = array();
if ($type=="row"){
  $ndata = mysql_fetch_assoc($data);
} elseif ($type=="array"){
  while($row = mysql_fetch_assoc($data)){
    $ndata[] = $row;
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消