dongyin6576 2019-03-15 11:23
浏览 25

php procedural用关联数组准备

I need to use a prepare statement to get an array of results, and my experience till now is all in procedural way. This is the code where I need a hand:

Before this I have a require statement with a connection established to the db, and with a $_POST method from a form I get two variables value that are $town and $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);

It doesn't run, of course, since in the "if" and in the "while" statements my syntax comes from non prepared statements and it's surely wrong.

I need to echo the rows of the result (I mean the three columns of the result).

In a non prepared statement I'd write like this:

$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);

What's the correct syntax to get the same in a prepared way? Thank you.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大