【问题标题】:I want fetch MySQLi table info into an HTML table using PHP, but mysqli_fetch_array query returns null我想使用 PHP 将 MySQLi 表信息提取到 HTML 表中,但 mysqli_fetch_array 查询返回 null
【发布时间】:2019-02-22 17:43:57
【问题描述】:

标题说明了一切。我也尝试使用 mysqli_fetch_assoc 但它也返回 null !与数据库的连接不是问题,因为它已经完成并且“用户”拥有所有权限。请看一下代码,如果可以的话,请帮助我...

<?php
    require 'connect.php';
    $sql = mysqli_query("select * from 'info' order by id desc");
    ?>
    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>List</title>
        <link rel="stylesheet" href="member_list.css">
    </head>
    <body>

    <h1>Users</h1>
    <table class="rwd-table">
        <tr>
            <th>Number</th>
            <th>Name</th>
            <th>Family</th>
        </tr>
        <?php
            while($row = mysqli_fetch_assoc($sql)):
            $i= 1;
            ?>
        <tr>
            <td data-th="Number"><?php echo $i; ?></td>
            <td data-th="Name"><?php echo $row['name']; ?></td>
            <td data-th="Family"><?php echo $row['family']; ?></td>
        </tr>
        <?php
        $i++;
        endwhile; ?>
    </table>
    </body>
    </html>

这里是连接文件:

<?php
/**
 * Created by PhpStorm.
 * User: reza
 * Date: 2019-02-22
 * Time: 18:57
 */

session_start();
ob_start();

$host = "localhost";
$username = "db2u";
$password = "1234";
$database = "db2";

$conn = mysqli_connect("$host" , "$username" , "$password" , "$database");

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

【问题讨论】:

  • throwing an error吗?另外(我的 php 有点生疏)mysqli_query() 中的第一个参数不应该是 `mysqli_connect` 对象吗?我认为这里丢失已久的错误会表明,如果是这样的话。

标签: php arrays database mysqli fetch


【解决方案1】:

mysqli_query 中传递$conn 并且表名应该在反引号(`)中,键与键盘上的波浪号键,而不是单引号。

$sql = mysqli_query($conn, "select * from `info` order by id desc");

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2019-06-10
    • 1970-01-01
    • 2023-03-07
    • 2014-01-13
    • 2016-12-30
    相关资源
    最近更新 更多