php - 准备好的陈述。执行后绑定是否正确?

php - prepared statements. Is it correct to bind after execution?

关于 php 中的 "prepared statements",
我在 php 的官方文档中找到了 here 这段我不理解的代码。

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

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

我总是先看到 "binding",然后才看到 "execution"。

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

谢谢。

绑定"parameters"和绑定"results"是两个不同的东西。

你必须在执行前绑定参数,因为参数将在执行过程中使用。

然而,您在执行后在 $stmt 对象中得到了结果,如果您想使用结果中的列,则将结果绑定到变量。

步骤如下:

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

    $stmt->bind_param(...)

  2. 执行准备好的语句

    $stmt->执行()

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

    $stmt->bind_result(...)