繁体   English   中英

使用php从数据库获取数据到html表

[英]getting data from database to html table using php

我想从数据库中获取数据并将其插入到我的html表中,这是我的代码,我不知道我的错误在哪里:

    <div class="ibox-content">
    <?php
    $servername = "localhost";
    $username = "sehnoqta_userbmc";
    $password = "u?gQ=uS%t;a?";
    $dbname = "sehnoqta_bmc";
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "SELECT name, lastname, phone FROM regis";
    $result = $conn->query($sql);
    $conn->close();
    ?>
            <table dir="rtl" class="table table-striped table-bordered table-hover dataTables-example" >
            <thead>
            <tr>
                <th>Name</th>
                <th>Last Name</th>
                <th>Phone</th>
                <th>Email</th>
                <th>Acc Type</th>
            </tr>
            </thead>
            <tbody>
            <?php
            if ($result->num_rows > 0) {

         while($row = $result->fetch_assoc()) {
             echo
              "<tr><td>" . $row["name"]. "</td>
              <td>" . $row["lastname"]. "</td>
              <td>" . $row["phone"]. "</td></tr>";
              <td>0795934799</td>
              <td class="center">demo@demo.com</td>
              <td>Admin</td>
         }
         echo "</table>";
    } 
        ?>
            </tbody>
            </table>
            </div>

我从数据库中获取的数据显示在表外。 对不起,英语不好:)

while循环中只有一个语法错误。 您需要在echo命令中打印所有内容。 如您所见,您具有echo(...); 其次是仍在php中的HTML。 因此,您应该通过将其更改为以下内容来进行更正。

while($row = $result->fetch_assoc()) {
         echo
          "<tr><td>" . $row["name"]. "</td>
          <td>" . $row["lastname"]. "</td>
          <td>" . $row["phone"]. "</td></tr>
          <td>0795934799</td>
          <td class=\"center\">demo@demo.com</td>
          <td>Admin</td>";
     }

可以使用的好资源是phpchecker.com ,它可以检查您的代码是否有错误

这是有效的代码...

    <?php
    $servername = "localhost";
    $username = "sehnoqta_userbmc";
    $password = "u?gQ=uS%t;a?";
    $dbname = "sehnoqta_bmc";
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "SELECT name, lastname, phone FROM regis";
    $result = $conn->query($sql);
    $conn->close();
    ?>
            <table dir="rtl" class="table table-striped table-bordered table-hover dataTables-example" >
            <thead>
            <tr>
                <th>Name</th>
                <th>Last Name</th>
                <th>Phone</th>
                <th>Email</th>
                <th>Acc Type</th>
            </tr>
            </thead>
            <tbody>
            <?php
            if ($result->num_rows > 0) {

         while($row = $result->fetch_assoc()) {
             echo
              "<tr><td>" . $row["name"]. "</td>
              <td>" . $row["lastname"]. "</td>
              <td>" . $row["phone"]. "</td>";
              "<td>0795934799</td>";
              "<td class='center'>demo@demo.com</td>";
              "<td>Admin</td></tr>";
         }
     echo "</table>";
 }

希望这可以帮助......

         echo
          "<tr><td>" . $row["name"]. "</td>
          <td>" . $row["lastname"]. "</td>
          <td>" . $row["phone"]. "</td>
          <td>0795934799</td>
          <td class='center'>demo@demo.com</td>
          <td>Admin</td></tr>";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM