簡體   English   中英

用來自mysql的數據填充HTML表格會產生空的表格單元格

[英]Populating HTML Table with Data from mysql Gives Empty Table Cells

我想用來自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();
?>

我認為可能缺少代碼,非常感謝您能給我任何幫助!

您已在while循環聲明中使用$row而不是$rows

while($rows = $result->fetch_assoc()){
    echo"<tr>
           <td>{$rows['name']}</td>
           <td>{$rows['lastname']}</td>
           <td>{$rows['email']}</td>
           <td align='center'><a href='update.php?id={$rows['id']}'>update</a></td>
         </tr>"
}

嘗試這個...

<?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();
?>

我的解決方案是您在下面添加

$sql = "SELECT * FROM $tbl_name";
$result = $con->query($sql);

var_dump($ result); die; //包括這行代碼,並查看它是否實際上是從數據庫中獲取的內容。 如果您可以在var_dump()中獲取這些記錄,則可以告訴下一步該做什么

您遇到的一個問題是變量名稱不同

這是變量$row

while($row = $result->fetch_assoc()){

這是變量$rows

<td><? echo $rows['name']; ?></td>

我希望至少可以在某個地方發出警告

您使用$rows而不是$row這是您正確的代碼:

<tr>
    <td><? echo $row['name']; ?></td>
    <td><? echo $row['lastname']; ?></td>
    <td><? echo $row['email']; ?></td>
    <td align="center"><a href="update.php?id=<? echo $row['id']; ?>">update</a></td>
</tr>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM