php - php程序准备与关联数组
问题描述
我需要使用准备语句来获得一系列结果,到目前为止我的经验都是程序化的。这是我需要帮助的代码:
在此之前,我有一个与数据库建立连接的 require 语句,并使用表单中的 $_POST 方法,我得到两个变量值,即 $town 和 $type。
$stmt = mysqli_smt_init($conn);
if (mysqli_stmt_prepare($stmt, "SELECT town, type, code FROM tab_01 WHERE town = ? AND type LIKE ?");
mysqli_stmt_bind_param($stmt, "ss", $town, $type);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$townvalue[] = $row["town"];
$typevalue[] = $row["type"];
}
}
mysqli_stmt_close($stmt);
当然,它不会运行,因为在“if”和“while”语句中,我的语法来自非准备好的语句,而且肯定是错误的。
我需要回显结果的行(我的意思是结果的三列)。
在未准备好的声明中,我会这样写:
$sql = "SELECT town, type, code FROM tab_01 WHERE town='$town' AND type LIKE '$type'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo $row["town"];
echo $row["type"];
echo $row["code"];
}
}
mysqli_free_result($result);
mysqli_close($conn);
以准备好的方式获得相同的正确语法是什么?谢谢你。
解决方案
推荐阅读
- python - 我无法解决合法 XPATH 表达式上的“lxml.etree.XPathEvalError: Invalid expression”错误
- c - ac 客户端对 ac dll 函数的调用是异步的吗?
- r - 是否可以通过使用来自谷歌云存储的数据训练 tensroflow 模型而不上传它来在本地工作?
- php - UTF-8 符号未在我的网站、SQL 数据库、PHP 代码、utf8mb4_general_ci 上正确显示
- c# - 如何使字符串中的粗体标签在屏幕上显示时生效?
- tensorflow - 如何将 tf 张量转换为 keras 可以适应的某种形式?
- javascript - 正确加入此正则表达式以确认密码
- javascript - 将 javascript 与库捆绑在一起
- php - 我无法请求数组数据
- neo4j - 如何根据 Cypher 中顶点属性的相等性检查创建边?