Not getting All Results from SQL Query

354 views Asked by At

I'm trying to build a query using Cookies (Favorited items)

<?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; 
    ?>

Where FavNum is somewhat redundant, it is simply counting the number of favorites incase there is 0 favorites I can have a fallback.

The query Echos out as the following:

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

As you can see the above code is 6 Numbers co-responding to a serial number I can copy and paste this Query directly into Navicat and the query will return 6 results.

CONTINUING ON:

<?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/>';
} ?>

This returns the following 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....

There are no other parts to the file. Right now I've taken my query php file and stripped it down to this specific portion of the file just to troubleshoot it... As you can see, I'm only getting 5 results. the Query Editor is getting 6 results.

So I did some testing I dont know if this helps narrow down the problem...

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

I'm getting a number back. of course its in loop. non stop forever, however I am getting a number back ... and yes... its the one I'm missing.

1

There are 1 answers

2
Erdo Dirgagautama On BEST ANSWER

Change your php code to below :

<?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/>';
    } 
?>

Or you could just do this :

<?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/>';
        } 
    }
?>

This should work.