Tanner B
Tanner B

Reputation: 53

How to Populate this HTML Table using PHP and MySQL

Mysql File:

    <?php
  function query($sql, $array) {

    $servername = "localhost";
    $username = "verifyUser";
    $password = "test";
    $dbname = "verify";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $result = $conn->query($sql);

    $stack = array();

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            array_push($stack, $row);
            if(!$array) {
                return $row;
            }
        }
        return $stack;
    }
    $conn->close();

}
?>

My php file:

    <table>
    <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Serial</th>
    </tr>

    <?php

        include_once("mysql.php");
        $result = query("SELECT ProductId, Name, SerialId FROM product", true);
        while ($row = mysqli_query($result)) {
            echo "<tr><td>" . $row['ProductId'] . "</td><td>" . $row['Name'] . "</td><td>" . $row['SerialId'] . "</td></tr>";
        }

    ?>
</table>

Output:

Id  Name    Serial

This is my first day trying to do anything with php. So go easy on me :) I've been following different tutorials and looking at the php documentation, but I have not be able to get my table to print anything besides the table headers. I don't think my query is wrong, because I return results in workbench https://gyazo.com/ab588fa7b7498444a5e8dcd1b172f315

Upvotes: 0

Views: 64

Answers (2)

Premlatha
Premlatha

Reputation: 1966

<?php
 $table='<table>
            <tr>
             <th>Id</th>
             <th>Name</th>
             <th>Serial</th>
           </tr>';


  $servername = "localhost";
  $username = "";
  $password = "";
  $dbname = "verify";

  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);

 // Check connection
 if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
  }

 $result = $conn->query($sql);

  $stack = array();

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    array_push($stack, $row);
    if(!$array) {
        return $row;
     }
 }
}

$conn->close();

 $table_content='';
 foreach($stack as $item)
 {
  $table_content.= "<tr>
                     <td>" . $row['ProductId'] . "</td>
                     <td>" . $row['Name'] . " </td>.  
                     <td>" . $row['SerialId'] . "</td>
                   </tr>";

 }
echo $table.$table_content.'</table>';
 ?>

Upvotes: 1

Russ J
Russ J

Reputation: 838

  <table>
<tr>
    <th>Id</th>
    <th>Name</th>
    <th>Serial</th>
</tr>

<?php


$servername = "localhost";
$username = "";
$password = "";
$dbname = "verify";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$result = $conn->query($sql);

$stack = array();

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        array_push($stack, $row);
        if(!$array) {
            return $row;
        }
    }
}

$conn->close();




foreach($stack as $item)
{
    echo "<tr><td>" . $row['ProductId'] . "</td><td>" . $row['Name'] . "</td>.  <td>" . $row['SerialId'] . "</td></tr>";
}




?>

Upvotes: 0

Related Questions