-2

Would anyone know how to get this result ? Basically I just need to display the columns as well.
Wanted Result image

Current result image


   <h1>
        Harry Potter Movies
    </h1>
    
<?php
     $hostname = "localhost"; 
     $dbUser = "root"; 
     $dbPassword = "root"; 
     $dbName = "harry potter movies";
     $port = 3306;

     $conn = mysqli_connect($hostname, $dbUser, $dbPassword, $dbName, $port);

     $sql = "SELECT * FROM Movies";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
       
        while($row = $result->fetch_assoc()) {
        
          echo $row["movie"]. "   " . $row["rating"]. "   " . $row["year"]. "<br>" ;
        }
        
      } else {
        echo "0 results";
      }

   ?> 
Shadow
  • 33,525
  • 10
  • 51
  • 64
Ben
  • 11

2 Answers2

0

You can use a table for that.

<?php
$hostname = "localhost"; 
$dbUser = "root"; 
$dbPassword = "root"; 
$dbName = "harry potter movies";
$port = 3306;

$conn = mysqli_connect($hostname, $dbUser, $dbPassword, $dbName, $port);

if (mysqli_connect_errno()) exit("Failed to connect to MySQL: " . mysqli_connect_error());
?>

<h1>Harry Potter Movies</h1>
    
<table>
    <thead>
        <tr>
            <th>Movie</th>
            <th>Rading</th>
            <th>Year</th>
        </tr>
    </thead>
    <tbody>
    <?php 
    $result = $conn->query("SELECT movie, rating, year FROM Movies");
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
        ?>
        <tr>
            <td><?= htmlentities($row["movie"], ENT_QUOTES) ?></td>
            <td><?= htmlentities($row["rating"], ENT_QUOTES) ?></td>
            <td><?= htmlentities($row["year"], ENT_QUOTES) ?></td>
        </tr>
        <?php
        }
    } else { ?>
        <tr>
            <td colspan="3">0 results</td>
        </tr>
        <?php
    }
    ?>
    </tbody>
</table>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0

What you want is something like this:

<table>
 <tr>
   <th>
     Movie
   </th>
   <th>
     Rating
   </th>
   <th>
     Year
   </th>
 </tr>
 <tr>
   <td>
     Harry Potter and the Philosopher's Stone
   </td>
   <td>
     7.6
   </td>
   <td>
     2001
   </td>
 </tr>

So your echo should ouptup some HTML tags with the content.

wesleynepo
  • 11
  • 1