Skip to content
Advertisement

Data from database only shows one data and it does not show

Our html/php code shows the name from the database but the rest of them doesn’t show. Although we used the same syntax for every part but we changed the data name same with the database column name.

Why is that so?

Here is our code:

<?php
  $servername = "localhost";
  $username = "";
  $password = "1234";
  $dbname = "straypaws";

  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  }
  $sql = "SELECT Dog_name, Dog_loc, Dog_desc1, Dog_desc2, Dog_info1 FROM dog_info";
  $result = $conn->query($sql);
?>

<div class="no-name-C61RwL helveticaneue-regular-normal-black-48px"><?php
if($row = mysqli_fetch_array($result)) {
    echo $row["Dog_name"];
} ?> 
</div>
<div class="dog-profile-C61RwL helveticaneue-regular-normal-black-72px">DOG PROFILE</div>
<img class="group-49-C61RwL animate-enter7" src="img/group-49@2x.svg" />
<div class="group-51-C61RwL">
  <div class="group-49-Hjsg7h">
    <div class="burnham-park-baguio-oKkELk helvetica55roman-regular-normal-black-24px"><?php
    if($row = mysqli_fetch_array($result)) {
        echo $row["Dog_loc"];
    } ?>

Advertisement

Answer

You can’t fetch same result multiple times.

Just store you fesult in array and then show it trought foreach

$dogs = [];
while($row = $result->fetch_assoc){
   $dogs[] = $row;
}

And then in you html:

foreach($dogs as $dog){
   echo $dog['Dog_name'];
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement