尝试从数据库打印结果时出现未定义的错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了尝试从数据库打印结果时出现未定义的错误相关的知识,希望对你有一定的参考价值。
我正在学习如何在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:xampphtdocsoutfitsave_outfit.test est.php:77 Stack trace: #0 C:xampphtdocsoutfitsave_outfit.test est.php(88): fetch_cids_outs(Object(mysqli_stmt), 151172293) #1 {main} thrown in C:xampphtdocsoutfitsave_outfit.test est.php on line 77
答案
我认为你的意思是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/>";
}
}
}
以上是关于尝试从数据库打印结果时出现未定义的错误的主要内容,如果未能解决你的问题,请参考以下文章
尝试在 SharePoint 环境中使用 AngularJS DateRangePicker 时出现未定义错误