当表有 10 行时,我的 PHP 查询返回零结果。实际显示的是部分PHP表格格式化代码。
实际输出:
0) { // output data of each row while($row = mysql_fetch_assoc($result)) { echo "in: " . $row["in"]. " - Name: " . $row["Name"]. " - Email: " . $row["Email"]. "
"; } } else { echo "0 re
sults"; } mysql_close($conn);?
HTML/PHP 代码:
<div class="about-text">
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
// Create connection
$conn = mysql_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysql_connect_error());
}
$sql = "SELECT * FROM tablename";
$result = mysql_query($conn, $sql);
if (mysql_num_rows($result) > 0) {
// output data of each row
while($row = mysql_fetch_assoc($result)) {
echo "in: " . $row["in"]. " - Name: " . $row["Name"]. " - Email: " . $row["Email"]. "<br>";
}
} else {
echo "0 results";
}
mysql_close($conn);
?>
</div>
最佳答案
http://php.net/manual/en/function.mysql-query.php
替换
$result = mysql_query($conn, $sql);
与
$result = mysql_query($sql, $conn);
关于当表有数据时,PHP MySQL 查询返回零结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32664321/