Let's say I have a menu with a category called animals. I access the MySQL database like so for the animals.
include_once 'config.inc';
$mysqli=new mysqli($mysql_host, $user, $password, $database);
$query="select distinct name from animals";
$res=$mysqli->query($query);
while ($row=$res->fetch_assoc()) {
print "{$row['name']}";
echo "</li>", "<li>";
I can get all the animals to print as list items with no problem. Now I want to use php to print out the animal that I click on. However, when I call this php file shown below, only the first list item will print properly. Please help
<?php
include_once 'config.inc';
$mysqli=new mysqli($mysql_host, $user, $password, $database);
$query="select distinct name from animals";
$res=$mysqli->query($query);
$row=$res->fetch_assoc();
echo "{$row['name']} selected";
Try like this :
if ($res = $mysqli->query($query)) {
while ($row = mysqli_fetch_assoc($res)) {
echo $row['name'];
}
}
I am not sure whether my answer is true or not, I am new in PHP, too.
Could you please try this one?
$query = "SELECT distinct name FROM animals";
$query_result = $sqli->query($query);
$query_row = $query_result->fetch_assoc();
$new = $query_row['name'];
echo $new;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.