1

What i try to do is check the user's Id by verifying in a cookie (called "identification").

thats my code:

if(!$noCookie) {

$sql = "SELECT * FROM `". TABLE_PREFIX ."_logged` WHERE `logged_cookie` = '". $usersCookie ."' LIMIT 1";
$query = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($query) <= 0) {
    echo 'yes cookie';

    $row = mysql_fetch_array($query) or die(mysql_error());
    print_r($row);

    while ($row=mysql_fetch_array($query)) {
        echo $usersId = $row["logged_user_id"];
    }
} else {
    echo 'no cookie';
}
}

What happends is that if the user's cookie is the same one like in the table, it will return the user's id.

so thats what im trying to do, the IF says "yes cookie", but inside the while it doesnt do anything! and its not giving me an error.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Elron
  • 11
  • 1
  • 2
  • 1
    Please read the manual entry for `mysql_fetch_array` to better understand what that function does: http://php.net/manual/en/function.mysql-fetch-array.php - and keep in mind: once a row fetched, it hops on the next one or returns false if there are no more rows. – hakre Jul 23 '11 at 17:52

3 Answers3

1

Since there are no rows, there's nothing to be fetched.

if (mysql_num_rows($query) <= 0) {

Should this be > 0?

Mika Tähtinen
  • 963
  • 7
  • 10
1

You only ran mysql_fetch_array when mysql_num_rows($query) evaluated to 0 or less; i.e. there are no rows.

Perhaps you meant:

if (mysql_num_rows($query) > 0) { // <-----
    echo 'yes cookie';

    $row = mysql_fetch_array($query) or die(mysql_error());
    // ...
}
else {
    echo 'no cookie';
}

Also note that you're running mysql_fetch_array in two different places, which is normally not right. Your while loop will not include the first result row, because you already did mysql_fetch_array once before the loop.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1
if(!$noCookie) {

$sql = "SELECT * FROM `". TABLE_PREFIX ."_logged` WHERE `logged_cookie` = '". $usersCookie ."' LIMIT 1";
$query = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($query) > 0) {
    echo 'yes cookie';

    $row = mysql_fetch_array($query) or die(mysql_error());   
    foreach ($row as $r)
       echo $r["logged_user_id"];
} else {
    echo 'no cookie';
}
}
yoda
  • 10,834
  • 19
  • 64
  • 92