警告:第31行的C:\ xampp \ htdocs \ connection \ index.php中的非法字符串偏移'name' 1“,
请任何人帮助我。
<?php
include('config.php');
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
$rows = mysqli_fetch_assoc($result);
?>
和Body标签
<?php
foreach ($rows as $row) {
echo $row['name'];
}?>
答案 0 :(得分:0)
您应该使用mysqli_fetch_all()一次性获取$rows
内的所有行。
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn);
$rows = mysqli_fetch_all($result);
echo "<pre>";
print_r($rows); // Check your result
答案 1 :(得分:0)
您可以将结果存储在数组中,并在正文中使用它,如下所示:
<?php
include('config.php');
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
$rows = array();
while($row = mysqli_fetch_assoc($result)){
$rows[] = $row;
}
和Body标签
<?php
foreach ($rows as $row) {
echo $row['name'];
}?>