[英]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"]". 我想知道我在做错什么。我似乎无法弄清为什么它不返回我的行,只是没有回显语句,而是在我调用“ $ grow [” bName“]”时将其留空。 I tried my query and it works fine, it returns two columns, username and bName.
我试过我的查询,它工作正常,它返回两列,用户名和bName。 Here is my php code:
这是我的PHP代码:
<?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 请尝试
$grow[0]["bName"]
否则您可以使用此功能
while($row = mysqli_fetch_assoc($gres))
{
$row['bNAme'];
}
first check num rows. 首先检查num行。 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) . (代表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"]
). 我问老师,她说会话可能已超时,因此在我调用它时给了我一个空字符串(
$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. 我将实现一些代码来检查
$_SESSION["username"]
是否为空,如果为空,则重定向回登录页面。 I'll put this on all pages. 我将其放在所有页面上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.