没有从SQL查询获得所有结果

时间:2013-07-23 03:43:46

标签: php mysql cookies missing-data missing-cookies

我正在尝试使用Cookie(收藏项目)构建查询

<?php

    $favnum=0;
$FavList = 'WHERE ';
foreach ($_COOKIE as $name => $value) {
                   if ($value ==  '1'){
                    if($name != 'PHPSESSID'){
                        $FavList .= 'num = "'.$name.'" OR ';
            $favnum++;
                    }
               }
            }
$FavList = substr($FavList, 0,-3).' ';
$FavList = 'SELECT * FROM RETS '.$FavList; 
    ?>

FavNum在某种程度上是多余的,它只是计算收藏夹的数量,因为有0个收藏夹我可以有一个后备。

查询回显如下:

SELECT * FROM RETS WHERE 
num = "E2671855" OR 
num = "E2659557" OR 
num = "E2689932" OR 
num = "E2670962" OR 
num = "E2684630" OR 
num = "E2677355"

正如您所看到的,上面的代码是6个与序列号共同响应的数字 我可以将此查询直接复制并粘贴到Navicat中,查询将返回6个结果。

继续:

<?php
$sql = $FavList;
    $results = mysql_query($sql) or die(mysql_error());
    $row = mysql_fetch_array($results) or die(mysql_error());

while($row = mysql_fetch_array($results)){
    echo 'Things that make me go Hmmm....<br/>';
} ?>

返回以下echo:

Things that make me go Hmm....
Things that make me go Hmm....
Things that make me go Hmm....
Things that make me go Hmm....
Things that make me go Hmm....

该文件没有其他部分。现在我已经把我的查询php文件并将其剥离到文件的这个特定部分只是为了解决它...正如你所看到的,我只得到5个结果。查询编辑器获得了6个结果。

所以我做了一些测试我不知道这是否有助于缩小问题...

while($row != mysql_fetch_array($results)) {echo '<script>alert(".$row['num'].")</script>';}

我收到了一个号码。当然是它的循环。永远不停,但我得到一个号码......是的......它是我失踪的那个。

1 个答案:

答案 0 :(得分:0)

将您的PHP代码更改为以下内容:

<?php
    $sql = $FavList;
    $results = mysql_query($sql) or die(mysql_error());
    $row = mysql_fetch_array($results) or die(mysql_error()); // this is the missing data
    echo 'Things that make me go Hmmm....<br/>'; // print the missing data

    while($row = mysql_fetch_array($results)){
        echo 'Things that make me go Hmmm....<br/>';
    } 
?>

或者你可以这样做:

<?php
    $sql = $FavList;
    $results = mysql_query($sql) or die(mysql_error());

    if (mysql_num_rows($result) > 0) {
        while($row = mysql_fetch_array($results)){ // all data will be fetch, including the missing data
            echo 'Things that make me go Hmmm....<br/>';
        } 
    }
?>

这应该有用。