开发者

while ($row=mysql_fetch_array($query)) is not working

开发者 https://www.devze.com 2023-03-22 16:39 出处:网络
What i try to do is check the user\'s Id by verifying in a cookie (called \"identification\"). thats my code:

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", b开发者_开发知识库ut inside the while it doesnt do anything! and its not giving me an error.


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

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

Should this be > 0?


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.


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

精彩评论

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

关注公众号