php - 从准备好的语句中获取关联数组
问题描述
我正在尝试使用准备好的语句从 SQL 查询中获取关联数组。我没有尝试任何工作。在执行准备好的语句之前,我能够让它工作。我正在阅读很多相互冲突的信息以及似乎不适用于过程代码的 OO 和 PDO 信息。我将来可能会切换到其中一种风格,但我不想现在就重写整个网站。任何帮助是极大的赞赏。我有一个 item_template.php,它使用 rows['name']、rows['type'] 等读取数据。它正在工作 mysqli_fetch_assoc ...我只是无法让 fetch_assoc 与准备好的语句一起工作。
if ($searchname == NULL) {
echo 'You must enter something to search for!';
}else{
$sql = "SELECT * FROM itemdb WHERE name LIKE ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: viewresults.php?error=sqlerror2");
exit();
}
else{
$searchname = "%".$searchname."%";
mysqli_stmt_bind_param($stmt,"s", $searchname);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$resultcheck = mysqli_stmt_num_rows($stmt);
echo $resultcheck;
if ($resultcheck == 0) {
echo 'No results found! Try again! ' . $resultcheck;
exit();
}else{
$result = mysqli_stmt_get_result($stmt);
echo $result;
while ($row = mysqli_fetch_assoc($stmt)) {
include("item_template.php");
echo "SUCCESS";
}
}
}
}
解决方案
您在数据库中使用什么编码?我使用utf8_general_ci
和西里尔字母。所以
mysqli_set_charset($conn, "utf8");
在$conn=mysqli_connect();
帮助我之后
推荐阅读
- mysql - 选择不包括其他字段的简单方法
- linux - 仅获取父文件夹的 git 差异
- javascript - 保存来自多个表单的值
- php - 如何查看json数组是否包含值?
- c++ - 如何在 C++ Lambda 中捕获向量中的元素?
- c# - C# WebSocket - 无法连接到 C# FleckWebSocket
- flutter - Webview 自动填充用户名和电子邮件字段自动填充会话值
- c++ - 比较函数导致向量排序的向量问题[leetcode]
- google-bigquery - 如何在视图或计划查询之间进行选择,以对通过 Stitch 导入的 BigQuery 表进行重复数据删除?
- css - 同一设备型号上的不同布局结果