I'm working on a website and it has a category dropdown. So my database has tabled called "category", and it has four items in there:
category
id name
1 first
2 second
3 thrid
4 fourth
And I'm trying to get all this info into an array, so I can use it in my HTML. But I'm getting a conversion error when I try this:
<div class="dropdown">
<?php
$db = new mysqli('localhost', 'root', 'password', 'db');
$query = "SELECT id, name FROM category";
$statement = $db->prepare($query);
$statement->execute();
$result = $statement->get_result();
$categories = $result->fetch_all(MYSQLI_ASSOC);
?>
<p class="dropdownSelect">Select Category</p>
<div class="dropdownContent">
<?php
foreach ($categories as $id => $name) {
echo "<p><a href=\"category.php?id=$id\"> $name</a></p>";
}
?>
</div>
</div>
</div>
The error I'm getting is:
Notice: Array to string conversion (path) on line 35
Line 35 is where the echo statement is. So when I researched, the suggested solution was to use print_r and var_dump but both didn't work in my case, and it just showed the same error message. What am I doing wrong here?