在html / php表中显示mysql数据库的结果 [英] Displaying results from mysql database in a html/php table

查看:145
本文介绍了在html / php表中显示mysql数据库的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我有一个页面可以查看从mysql数据库输出的数据。它运作良好。但它不够时尚。所以我决定放入一个html < table> 标签。然而,它并没有显示数据。



我试过放入

< ; TD> < / td> 等行之间的代码,但它会停止解析代码。

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

//创建连接
$ conn = new mysqli($ servername,$ username,$ password,$ dbname);
//检查连接
if($ conn-> connect_error){
die(Connection failed:。$ conn-> connect_error);
}

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



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

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

while($ row = $ result-> fetch_assoc()){
echo
< br>。 $行[ FIRST_NAME。
。 $ row [last_name]。
。 $ row [email]。;
}
}
else {
echo0 results;
}

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


解决方案

试试这个,如果它适合你

  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]。 < / TD> 中;
回显< td>。 $ row [last_name]。 < / 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>";

这篇关于在html / php表中显示mysql数据库的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆