Saravana Saravana -4 years ago 122
PHP Question

Php Mysqli_fetch_assoc Error : "Warning: Illegal string offset 'name' in"


Warning: Illegal string offset 'name' in C:\xampp\htdocs\connection\index.php on line 31
1",


Please can anyone help me.

<?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);

?>


and in Body tag

<?php
foreach ($rows as $row) {
echo $row['name'];
}?>

Answer Source

You should use mysqli_fetch_all() here to get all rows within $rows at once.

$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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download