Py学习  »  DATABASE

$row未正确回显mySql数据库中的行

StrangeJmaster • 4 年前 • 914 次点击  

我在一个网站上工作,想显示用户的帖子。我知道如何在数据库和所有东西上创建行,但出于某种原因,它只是不加载任何东西 我在顶部有一个mysqli连接代码。

以下是PHP:

$sql = "SELECT MainPagePosts, PostUser FROM UserPosts";
$result = $mysqli->query($sql);


echo '<div class="UserPostsBox">';
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
         echo '<p class="UserPost">'; $row["MainPagePosts"]; echo " | Posted By: "; $row["PostUser"]; echo"</p>";
     }
echo '</div>';
} else {
    echo "0 results";
}
$mysqli->close();

?>

她的CSS:

.UserPostsBox {
  position: absolute;
  z-index: 3;
  top: 450px;
  left: 480px;
  width: 350px;
  height: 550px;
  border: 3px solid;
  border-color: orange;
  overflow-y: auto;
}
.UserPost {
  color: #22262e;
  font-size: 30px;
  font-family: sans-serif;
  z-index: 3;
  width: auto;
  height: auto;
  position: relative;
  top: auto;
  bottom: auto;
  left: auto;
  overflow: auto;
  border: 3px solid;
  border-color: #22262e;
}

我试了很多,不知道该怎么办。 谢谢您

如果你想看网站,请点击这里 https://johnpowers.000webhostapp.com/JohnSite/HomePageItems/JohnSite_HomePage.html

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50940
 
914 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Nick
Reply   •   1 楼
Nick    4 年前

你的问题在于 echo 声明。你在试图回应 $row 只需调用值(例如。 $row["MainPagePosts"]; ),但是那实际上什么也做不了,你需要 回声 他们也是。改变

echo '<p class="UserPost">'; $row["MainPagePosts"]; echo " | Posted By: "; $row["PostUser"]; echo"</p>";

to(使用字符串连接运算符 . ):

echo '<p class="UserPost">' . $row["MainPagePosts"] . " | Posted By: " . $row["PostUser"] . "</p>";

它应该可以正常工作。