致命错误:调用未定义函数fetch_assoc()有什么用? [英] Fatal error: Call to undefined function fetch_assoc() What Gives?
本文介绍了致命错误:调用未定义函数fetch_assoc()有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在登录后从页面上的数据库中显示用户,但我不断收到错误消息:
Im trying to display users from the database on the page after login but i keep getting an error message:
致命错误:调用未定义函数fetch_assoc()
Fatal error: Call to undefined function 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>
推荐答案
这是您的操作方式,您在混合面向对象和过程:
This is how you do this, you're mixing object oriented and procedural:
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>";
}
这篇关于致命错误:调用未定义函数fetch_assoc()有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文