如何将php代码插入表格样式?

时间:2019-06-16 17:19:32

标签: php

我是PHP的超级新手,正在编写(或尝试编写)一些代码。我一直在尝试将php代码插入表数据单元格,但显然我在犯一些语法错误。你能告诉我这是什么吗?

<tr>
<td><?php echo $row["name"]; ?>
</td>
<td><?php echo $row["translator"]; ?>
</td>
<?php echo "<td style=\"background-image:url(./assets/images/series/.$row['cover'].); background-repeat:no-repeat; background-size:250px 180px; width: 250px; height: 180px;\">"
</td>
</tr>

1 个答案:

答案 0 :(得分:1)

$ row必须是PHP已知的。因此您需要执行以下操作:

<?php
$servername = "localhost"; //your db host
$username = "username";
$password = "password";
$dbname = "dbname";

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

$sql = "SELECT name, translator, cover FROM translators";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr>";
echo "<td>";
echo $row["name"];
echo "</td>";
echo "<td>";
echo $row["translator"];
echo "</td>";

echo "<td style='background-image:url("./assets/images/series/".$row['cover'].""); background-repeat:no-repeat; background-size:250px 180px; width: 250px; height: 180px;'>"
echo "</td>";
echo "</tr>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>