且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在html / php表中显示mysql数据库的结果

更新时间:2023-01-31 09:37:08

试试这个,如果它适合你

  echo< table border ='1'> 
< tr>
<第一名< / b>
< th>姓氏< />
< th>电子邮件< / th>
< / tr>

if($ result-> num_rows> 0){
//每行的输出数据

while($ row = $ result-> fetch_assoc()){
echo< tr>;
回显< td>。 $ row [first_name]。 &LT; / TD> 中;
回显< td>。 $ row [last_name]。 &LT; / TD> 中;
回显< td>。 $ row [email]。 < / td>;
回声< / tr>;
}
}
echo< / table>;


Hi I've got a page where I can view data output from a mysql database. It works good. But it's not stylish. So I decided to put in a html <table> tag. However it's not displaying the data.

I've tried putting in

<td> </td> etc between rows but it stops the code being parsed.

<?php
    $servername = "******";
    $username = "root";
    $password = "********";
    $dbname = "test2";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT first_name, last_name, email FROM person_list";
    $result = $conn->query($sql);



    //display table
    echo "<table border='1'>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Email</th>
    </tr>";

           if ($result->num_rows > 0) {
         // output data of each row

         while($row = $result->fetch_assoc()) {
           echo
     "<br>   ". $row["first_name"]. " "
                                   . $row["last_name"] ." "
                                    . $row["email"] ." " ;
        }
    }
     else {
         echo "0 results";
    }

    echo "</table>";
    $conn->close();
    ?>

Try this one, if it works for you..

echo "<table border='1'>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Email</th>
    </tr>";

       if ($result->num_rows > 0) {
     // output data of each row

     while($row = $result->fetch_assoc()) {
       echo "<tr>";
       echo "<td>". $row["first_name"] . "</td>";
       echo "<td>". $row["last_name"] . "</td>";
       echo "<td>". $row["email"] . "</td> " ;
       echo "</tr>";
    }
}
echo "</table>";