$ query =选择 并无法打印查询结果。当我删除if($ results == true)时,网页将运行。但是一旦放置了if语句,它将不再加载。
数据库图访问https://www2.cs.uregina.ca/~webstesa/a2/2019-10-erd.png
<?php
session_start();
//If nobody is logged in, display login and signup page.
if(isset($_SESSION["email"]))
{
$email = $_SESSION["email"];
?>
<!DOCTYPE html>
...
</nav>
</br>
<?php
$db = new mysqli("localhost", "xxxxxxxx", "xxxxxxxx", "xxxxxxxx");
if ($db->connect_error)
{
die ("Connection failed: " . $db->connect_error);
}
else
{
$query = "SELECT question.question, question.create_dt
FROM question INNER JOIN User
ON question.user_ID = User.user_Id
WHERE User email = '".$_SESSION["email"]."'";
$result = $db->query($query);
$num_row = $result -> num_row;
echo $num_row;
if ($result == true)
{
if ($row = $result -> fetch_assoc())
{
print (.$row["question"]);
print ("Date Asked: " .$row["create_dt"] "<br>");
print ("<div class = qb");
print (<"<br>");
}
}
}
$db->close();
?>
</body>
</html>
<?php }
else {
echo "<a href='Login.php'>LogIn</a>";
}
?>
我正在尝试介绍用户提出的5个最新问题。我已设置页面,以便用户登录,如果不是,则将他们重定向到登录页面。一切正常,但我似乎无法加载问题。
答案 0 :(得分:0)
您只需要检查if ($result)
,就不需要任何条件。
试试这个代码
if ($result)
{
if ($row = $result -> fetch_assoc())
{
print (.$row["question"]);
print ("Date Asked: " .$row["create_dt"] "<br>");
print ("<div class = 'qb'");
print (<"<br>");
}
}
在某些情况下,您不能使用the GLOBAL variable
,而应将其存储在变量中并在查询中使用。
尝试此查询
$email = $_SESSION["email"];
$query = "SELECT question.question, question.create_dt
FROM question INNER JOIN User
ON question.user_ID = User.user_Id
WHERE User email = '$email'";
$result = $db->query($query);
答案 1 :(得分:0)
我认为您的查询字符串不正确,请尝试以下查询字符串-
$query = "SELECT question.question, question.create_dt
FROM question INNER JOIN User
ON question.user_ID = User.user_Id
WHERE User email = ".$_SESSION['email'];
$result = $db->query($query);
$num_row = $result -> num_row;
echo $num_row;
if ($result){
if ($row = $result -> fetch_assoc()){
print (.$row["question"]);
print ("Date Asked: " .$row["create_dt"] "<br>");
print ("<div class = qb");
print (<"<br>");
}
}