Arevshatyan Gegham Arevshatyan Gegham - 1 year ago 59
MySQL Question

I want to get all of my database items

I've included this

file in my
, but I only get the first item from the database. How can I get all items from my database?

$sql = "SELECT name,price FROM items ORDER BY `id`";

$res = mysqli_query($conn, $sql);
$page = mysqli_fetch_assoc($res);


Answer Source

As you said you want all records from the database, so you need to use while loop like below:-

   $sql = "SELECT name,price FROM items ORDER BY `id`"; // query
   $res = mysqli_query($conn, $sql); // execute query
   while($page = mysqli_fetch_assoc($res)){ // loop
      echo "<h2>". $page["name"]."</h2>".$page["price"]; // print all record (You can change pattern of printing according to your wish )
   mysqli_close($conn); // close connection
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download