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

时间:2016-10-31 08:11:31

标签: php html mysql

我想从数据库中获取数据并将其插入到我的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>

从数据库中获取的数据显示在表外。 抱歉英文不好:)

3 个答案:

答案 0 :(得分:0)

你的while循环只是一个语法错误。您需要在echo命令中打印所有内容。正如你在你的内容中看到的,你有echo(...);后面还有一些HTML仍然在php里面。因此,您应该通过将其更改为以下内容来更正它。

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资源可以检查您的代码是否存在错误

答案 1 :(得分:0)

这是有效的代码......

    <?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>";
 }

希望这会有所帮助......

答案 2 :(得分:0)

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