我尝试在登录后从页面上的数据库中显示用户,但我不断收到错误消息:
致命错误:调用未定义的函数fetch_assoc()
这是代码:
<?php
session_start(); ?>
<link rel="stylesheet" href="form.css">
<div class="body content">
<div class="welcome">
<div class="alert alert-success"><?= $_SESSION['message'] ?></div>
<img src="<?= $_SESSION['avatar'] ?>"><br />
Welcome <span class="user"><?= $_SESSION['username'] ?></span>
<?php
$mysqli = new mysqli("localhost","user","password","database");
//Select queries return a resultset
$sql = "SELECT username, avatar FROM users";
$result = $mysqli->query($sql); //$result = mysqli_result object
//var_dump($result);
?>
<div id='registered'>
<span>All registered users:</span>
<?php
//while($row = $result->fetch_assoc()){ //returns associative array of fetched row
while($row = fetch_assoc($result)){ //returns associative array of fetched row
//echo '<pre>';
//print_r($row);
//echo '</pre>';
echo "<div class='userlist'><span>$row[username]</span>";
echo "<img src='$row[avatar]'></div>";
}
?>
</div>
</div>
</div>
答案 0 :(得分:0)
这就是你如何做到这一点,你正在混合面向对象和程序:
while($row = $result->fetch_assoc()){ //returns associative array of fetched row
//echo '<pre>';
//print_r($row);
//echo '</pre>';
echo "<div class='userlist'><span>$row[username]</span>";
echo "<img src='$row[avatar]'></div>";
}
答案 1 :(得分:0)
以下是与数据库进行过程连接的正确脚本:
<?php
$con = mysqli_connect("localhost","root","","db1");
$sql = "SELECT username, avatar FROM users";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($result);
echo $row["username"] . " " . $row["avatar"] . "<br>";
mysqli_close($con);
?>