jet2016
jet2016

Reputation: 397

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

Regarding "prepared statements" in php,
I have found here in the official documentation of php this piece of code that I don´t understand.

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

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

I had always seen the "binding" first and then, the "execution".

Could anybody tell me why in this case, is the other way around?

Thanks.

Upvotes: 3

Views: 259

Answers (1)

Adem Tepe
Adem Tepe

Reputation: 768

Binding "parameters" and binding "results" are two different things.

You have to bind parameters before execution because parameters are going to be used during execution.

Yet, you have the result in $stmt object after execution, and if you want to use columns from the result, you bind results to variables.

So here are the steps:

  1. bind parameters to $stmt object

    $stmt->bind_param(...)

  2. execute prepared statement

    $stmt->execute()

  3. bind columns from the result to variables

    $stmt->bind_result(...)

Upvotes: 1

Related Questions