简体   繁体   中英

PHP mysqli query is not returning mysqli_result object on SELECT statement

I am wondering what I am doing wrong.. I can't seem to figure out why it's not returning back my rows, it's just not echoing out the statements and leaving it blank when I call to "$grow["bName"]". I tried my query and it works fine, it returns two columns, username and bName. Here is my php code:

<?php
require ("cfd.php");
require ("header.php");
$username = $_SESSION["username"];
echo $username;
    //run the query
    $gsql = "SELECT babysitter.bName, login.username FROM login INNER JOIN babysitter ON login.b_ID=babysitter.b_ID WHERE login.username = '".$username."';";
    $gres = mysqli_query($db, $gsql);
    $grow = mysqli_fetch_assoc($gres);
    echo "<h2>Hello ".$grow["bName"].", Your admin status is ".$_SESSION['admin']."<br>";
    echo $gres->num_rows;
?>


<?php
require("footer.php");
?>

Please try $grow[0]["bName"] OR else you can use this

while($row = mysqli_fetch_assoc($gres)) 
{
    $row['bNAme'];
}

first check num rows. And remove ; at the end of the query

Try this

if (mysqli_num_rows($gres) > 0) {
 while ($row = mysqli_fetch_assoc($gres)) {
    echo $row["bName"];

  }

}

Your query should be

 $gsql = "SELECT babysitter.bName, login.username FROM login INNER JOIN babysitter ON login.b_ID=babysitter.b_ID WHERE login.username = '".$username."'";

Use this

if (mysqli_num_rows($gres) > 0) {
 while ($row = mysqli_fetch_assoc($gres)) {
    echo $row["bName"];

  }

}

(Posted on behalf of the OP) .

I asked my teacher and she said that the session could have timed out, therefore giving me an empty string when I called to it ( $username = $_SESSION["username"] ). It then would insert nothing into the query and would give me an error.

I am going to implement some code to check if the $_SESSION["username"] is empty or not, if it is, then redirect back to the login page. I'll put this on all pages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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