php - 用来自 mysql 的数据填充 HTML 表格给出空表格单元格

标签 php html mysql

我想用 PHP 将 MySQL 数据库中的数据填充到一个表中, 但是在执行代码时表格单元格仍然是空的,我没有收到任何错误

代码如下:

<?php
$host = "localhost"; // Host name 
$username = ""; // Mysql username 
$password = ""; // Mysql password 
$db_name = "test"; // Database name 
$tbl_name = "test_mysql"; // Table name 
$server_name = "localhost";

// Create connection
$con = new mysqli($server_name, $username, $password, $db_name, 3306);
if($con->connect_error){
    die("Connection failed: ".$con->connect_error);
}

// Check connection
if($con->connect_error){
    die("Connection failed: ".$conn->connect_error);
}

$sql = "SELECT * FROM $tbl_name";
$result = $con->query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
        <td>
            <table width="400" border="1" cellspacing="0" cellpadding="3">
                <tr>
                    <td colspan="4"><strong>List data from mysql</strong></td>
                </tr>
                <tr>
                    <td align="center"><strong>Name</strong></td>
                    <td align="center"><strong>Lastname</strong></td>
                    <td align="center"><strong>Email</strong></td>
                    <td align="center"><strong>Update</strong></td>
                </tr>
                <?php
                if($result->num_rows > 0){
                    // output data of each row
                    while($row = $result->fetch_assoc()){ ?>
                        <tr>
                            <td><? echo $rows['name']; ?></td>
                            <td><? echo $rows['lastname']; ?></td>
                            <td><? echo $rows['email']; ?></td>
                            <td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td>
                        </tr>
                        <?php
                    }
                }
                ?>
            </table>
        </td>
    </tr>
</table>
<?php
$con->close();
?>

我认为它可能缺少代码,非常感谢您能给我的任何帮助!

最佳答案

试试这个...

<?php
$host = "localhost"; // Host name 
$username = ""; // Mysql username 
$password = ""; // Mysql password 
$db_name = "test"; // Database name 
$tbl_name = "test_mysql"; // Table name 
$server_name = "localhost";

// Create connection
$con = new mysqli($server_name, $username, $password, $db_name, 3306);
if($con->connect_error){
   die("Connection failed: ".$con->connect_error);
}

// Check connection
if($con->connect_error){
 die("Connection failed: ".$conn->connect_error);
}

$sql = "SELECT * FROM $tbl_name";
$result = $con->query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
    <td>
        <table width="400" border="1" cellspacing="0" cellpadding="3">
            <tr>
                <td colspan="4"><strong>List data from mysql</strong></td>
            </tr>
            <tr>
                <td align="center"><strong>Name</strong></td>
                <td align="center"><strong>Lastname</strong></td>
                <td align="center"><strong>Email</strong></td>
                <td align="center"><strong>Update</strong></td>
            </tr>
            <?php
            if($result->num_rows > 0){
                // output data of each row
                while($rows = $result->fetch_assoc()){ ?>
                    <tr>
                        <td><?php echo $rows['name']; ?></td>
                        <td><?php echo $rows['lastname']; ?></td>
                        <td><?php echo $rows['email']; ?></td>
                        <td align="center"><a href="update.php?id=<?php echo $rows['id']; ?>">update</a></td>
                    </tr>
                    <?php
                }
            }
            ?>
        </table>
    </td>
</tr>
</table>
<?php
$con->close();
?>

关于php - 用来自 mysql 的数据填充 HTML 表格给出空表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31388719/

相关文章:

php - 逐步查询 MySQL 数据库 - 最佳方法?

php - 我的简单搜索引擎遇到问题

使用 Like 的 MySQL 参数化查询

mysql - 避免在 Python 中使用游标

php - 尝试与房间匹配

html - 试图理解 CSS 和 Box-sizing : Border-box property

java - 如何获取 webtable 的动态 Xpath?

html - 使用文本对齐将 HTML 表格导出为 PDF

mysql - 在 SQL 中计算百分比

php - 处理混合 utf8 和 utf8mb4 MYSQLI & PHP