尝试从数据库打印结果时出现未定义的错误

问题描述 投票:2回答:1

我正在学习如何在php端进行数据库编码。我成功地将信息插入到我的数据库中,但是我无法从中获取信息。如何打印数据?没什么好看的,我想知道我们得到数据的原始方式,所以像print_r。这是我的代码:

<?php

$conn = mysqli_connect($servername, $dBUsername, $dbPassword, $dbName);
$stmt = mysqli_stmt_init($conn);
$result = fetch_ids_outs($stmt, $id);

function fetch_ids_outs($stmt, $id) {

    $userID = search_for_user($stmt, $id);
    if ($userID == false) return "User not in Database";

    // Otherwise get the data
    $sql = "SELECT * FROM users WHERE user_id = ?";
    if(!mysqli_stmt_prepare($stmt, $sql)) {
        return false;
    } else {
        mysqli_stmt_bind_param($stmt, "i", $userID);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_store_result($stmt);
        while($row = $stmt->fetch_array()) {
          echo $row['name'];
          echo "<br/>";
        }
    }
}

错误:

Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::fetch_array() in C:\xampp\htdocs\outfit\save_outfit.test\test.php:77 Stack trace: #0 C:\xampp\htdocs\outfit\save_outfit.test\test.php(88): fetch_cids_outs(Object(mysqli_stmt), 151172293) #1 {main} thrown in C:\xampp\htdocs\outfit\save_outfit.test\test.php on line 77
php mysql mysqli
1个回答
1
投票

我认为你的意思是get_result(),而不是store_result()

这是一个例子:

function fetch_ids_outs($stmt, $id) {

    $userID = search_for_user($stmt, $id);
    if ($userID == false) return "User not in Database";

    // Otherwise get the data
    $sql = "SELECT * FROM users WHERE user_id = ?";
    if(!mysqli_stmt_prepare($stmt, $sql)) {
        return false;
    } else {
        mysqli_stmt_bind_param($stmt, "i", $userID);
        mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_get_result($stmt); // get result
        while($row = mysqli_fetch_assoc($result)) { // fetch by associative index
          echo $row['name'];
          echo "<br/>";
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.