我正在学习如何在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
最佳答案
我想你的意思是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/>";
}
}
}
关于php - 尝试从数据库打印结果时出现未定义的错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54456422/