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.

1
  • 1
    Please read the manual entry for mysql_fetch_array to better understand what that function does: 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. Commented Jul 23, 2011 at 17:52

3 Answers 3

1

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

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

Should this be > 0?

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.