我试图回应出数据库中设置的敏捷性数量。这是我当前的代码,如果你可以查看它,看看是否有任何我需要改变以使其工作。目前即将收到错误
注意:未定义的偏移量:
中的0
$dbserver = "localhost";
$dbusername = "root";
$dbpassword = "*******";
$db = "********";
//CREATE CONNECTION
$conn = new mysqli($dbserver, $dbusername, $dbpassword, $db);
$id = $_SESSION['loggedin'];
$query = "SELECT * FROM stats WHERE id='$id'";
$stmt = mysqli_query($conn, $query);
$result = mysqli_fetch_all($stmt,MYSQLI_ASSOC);
<div class="Agility">
<h2>Agility</h2>
<p><?php echo $result[0]['agility']; ?></p>
</div>
答案 0 :(得分:2)
首先,由于您的id字段可能是唯一的,因此您应该将查询限制为1。然后,在显示结果之前检查查询返回的行数,以防没有。
$query = "SELECT * FROM stats WHERE id='$id' LIMIT 1";
$stmt = mysqli_query($conn, $query);
if(mysqli_num_rows($stmt) > 0){
$result = mysqli_fetch_all($stmt,MYSQLI_ASSOC);
?>
<div class="Agility">
<h2>Agility</h2>
<p><?php echo $result[0]['agility']; ?></p>
</div>
<?php } else { ?>
<p>There are no results.</p>
<?php } ?>