update page now

Voting

The Note You're Voting On

Bernie van't Hof
14 years ago
Prepared statements are confusing in the beginning ..

mysqli->prepare() returns a so-called statement object which is used for subsequent operations eg execute, bind_param, store_result, bind_result, fetch, etc.

The statement object has private properties which update as each statement operation is carried out. I found these useful for understanding what is going on when writing a prepared statement function: 

affected_rows
insert_id
num_rows
param_count
field_count
errno
error
sqlstate
id

But it took a little time to get my head around accessing them:

<?php
$stmt = $mysqli->prepare($query);

//       .. $stmt-> operations ..

var_dump($stmt); // shows null values

var_dump($stmt->errno); // note literal, displays value 

//       .. $stmt-> operations ..

// to keep a copy ..
// get_object_properties() won't work
// clone() won't work
$properties = array();
foreach ($stmt as $name => $priv){
    $properties[$name] = $stmt->$name; //    works
    // $properties[$name] = $priv; //    won't work, foreach can't access private properties
}

$stmt->close();
// var_dump($stmt->errno) // won't work, $stmt is closed
?>

<< Back to user notes page

To Top