【问题标题】:php - prepared statements. Is it correct to bind after execution?php - 准备好的语句。执行后绑定是否正确?
【发布时间】:2018-11-17 16:30:26
【问题描述】:

关于 php 中的“准备好的语句”,
我在php的官方文档中找到了here这段代码我看不懂。

/* execute statement */
$stmt->execute();

/* bind result variables */
$stmt->bind_result($name, $code);

我总是先看到“绑定”,然后才是“执行”。

谁能告诉我为什么在这种情况下,是相反的方式吗?

谢谢。

【问题讨论】:

  • 没有。您需要在执行语句之前绑定参数
  • 执行前绑定参数,执行后绑定结果。
  • 感谢@Barmar 和其他所有人。所以我猜想“mysqli_stmt_bind_result”是用来和“mysqli_stmt_fetch”一起使用的。
  • 是的,它就是这样工作的。

标签: php sql prepared-statement


【解决方案1】:

绑定“参数”和绑定“结果”是两个不同的东西。

你必须在执行之前绑定参数,因为参数将在执行期间使用。

然而,执行后你在 $stmt 对象中有结果,如果你想使用结果中的列,你可以将结果绑定到变量。

以下是步骤:

  1. 将参数绑定到 $stmt 对象

    $stmt->bind_param(...)

  2. 执行准备好的语句

    $stmt->执行()

  3. 将结果中的列绑定到变量

    $stmt->bind_result(...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    相关资源
    最近更新 更多