【问题标题】:Get data from database with mysqli_fetch_array and mysqli_fetch_field [duplicate]使用 mysqli_fetch_array 和 mysqli_fetch_field 从数据库中获取数据 [重复]
【发布时间】:2021-06-14 09:41:45
【问题描述】:

我想用mysqli_fetch_arraymysqli_fetch_field 从我的数据库中查询数据,但它根本不起作用。 我有这样的tbl_student 表:

| id | firstname | lastname |
| -- | --------- | -------- |
| 1  |  first_A  |  last_A  |
| 2  |  first_B  |  last_B  |
| 3  |  first_C  |  last_C  |

PHP 代码:

$query = "select * from tbl_student";
$result = mysqli_query($db, $query);
while ($row = mysqli_fetch_array($result)) {
    while ($col = mysqli_fetch_field($result)) {
        echo $col->name . " = " . $row[$col->name];
        echo "<br>";
    }
    echo "<br>";
}

如我所愿,我想要这样的结果:

id = 1
firstname = first_A
lastname = last_A

id = 2
firstname = first_B
lastname = last_B

id = 3
firstname = first_C
lastname = last_C

但不是,我只有第一手记录:

id = 1
firstname = first_A
lastname = last_A

我该怎么做?

【问题讨论】:

    标签: php mysql mysqli


    【解决方案1】:

    mysqli_fetch_field 旨在获取有关查询中字段的详细信息,而不是获取有关记录中每个字段的信息。

    使用以下代码获得您想要的结果。

    while ($row = mysqli_fetch_array($result)) {
        foreach ($row as $key => $value) {
            echo $key . " = " . $value;
            echo "<br>";
        }
        echo "<br>";
    }
    

    【讨论】:

    • 哦,我没注意到。谢谢你!
    猜你喜欢
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 2020-11-13
    • 2016-07-18
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多