简体   繁体   English

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

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

i want to fetch data from database and insert it in my html table here is my code i dont know where is my misatake: 我想从数据库中获取数据并将其插入到我的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>

data i fetch from database shows out of table. 我从数据库中获取的数据显示在表外。 sorry for bad English :) 对不起,英语不好:)

Theres just a Syntax error in your while loop. while循环中只有一个语法错误。 You need to print everything inside the echo command. 您需要在echo命令中打印所有内容。 As you can see in yours, you have echo(...); 如您所见,您具有echo(...); followed by some HTML still inside the php. 其次是仍在php中的HTML。 So you should correct it by changing it to the following. 因此,您应该通过将其更改为以下内容来进行更正。

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

A good resource that you can use is phpchecker.com that checks your code for errors 可以使用的好资源是phpchecker.com ,它可以检查您的代码是否有错误

Here is the code that works... 这是有效的代码...

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

Hope this helps...... 希望这可以帮助......

         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