当表有数据时,PHP MySQL 查询返回零结果

标签 php html mysql

当表有 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/

相关文章:

php - 我尝试将其转换为与 Laravel 的 DB 类一起使用,但它没有返回任何内容

php - mysql无法插入8列的表

html - 如何在小屏幕上将侧边栏转换为汉堡菜单

php - 在网格中平铺缩略图

php - 在 CakePHP 中路由静态文件夹名称

php - 使用 PHP 进行动态 MySQL 查询

mysql - 根据同一个表中的数据选择数据

php合并查询左连接和if else语句

html - CSS 表 : Scrollbar for first column and auto width for remaining column

php - SQL 查询 - 获取行退出其他表 laravel 上的一些值