简体   繁体   English

mysqli_fetch_array没有结果

[英]mysqli_fetch_array no result

This is a part of my header file: 这是我的头文件的一部分:

    $username = $_COOKIE["empirebattles"];
    $userdata_query = mysqli_query($con,"SELECT * FROM users WHERE username = '$username'");
    $userdata = mysqli_fetch_array($userdata_query, MYSQLI_ASSOC);

I got the value of the cookie, ran a query with it and then fetched the array, why does it not seem to print the correct value when I write: 我得到了cookie的值,用它运行了一个查询,然后获取了数组,为什么我写的时候似乎没有打印正确的值:

echo $userdata["username"];

It's probably a small error, but all help is appreciated. 这可能是一个小错误,但所有帮助都表示赞赏。

Try this: 尝试这个:

   $username = isset($_COOKIE["empirebattles"]) ? $_COOKIE["empirebattles"] : false ;
    if($username){
        $userdata_query = mysqli_query($con,"SELECT * FROM users WHERE username = '$username'");
        $userdata = mysqli_fetch_array($userdata_query, MYSQLI_ASSOC);
        echo $userdata["username"];
    }
    else{

     echo "no username selected!";
    }

this may sound stupid, but have you included your connetion to the database, without a DB connection then you wont get a return. 这可能听起来很愚蠢,但你把你的连接包含在数据库中,没有数据库连接,那么你就不会得到回报。 before the query, make sure you have the following 在查询之前,请确保您具有以下内容

<?php include('Connection_To_Database.php') ?>

you maye have overlooked this 你可能忽视了这一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM