如何使用PHP和MySQL填充此HTML表

时间:2019-03-05 00:43:06

标签: php html mysql

Mysql文件:

11, 25, 7

我的php文件:

    <?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();

}
?>

输出:

    <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>

这是我尝试使用php做任何事情的第一天。所以对我轻松一点:) 我一直在遵循不同的教程,并查看php文档,但是我无法让我的表打印除表头之外的任何内容。 我认为查询没有错,因为我在工作台中返回结果 https://gyazo.com/ab588fa7b7498444a5e8dcd1b172f315

2 个答案:

答案 0 :(得分:1)

<?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>';
 ?>

答案 1 :(得分:0)

$ ./bin/binaryconversion
byte : 00000010
word : 0000000000000010