Sunday, August 3, 2014

mysqli Prepared statements using the SELECT syntax

 

$mysqli = new mysqli('localhost','username','password','databasename');
$query
= "SELECT id,name FROM customers WHERE id=?";

if ($stmt = $mysqli->prepare($query)) {
$stmt
->bind_param('s', $_id);
$stmt
->execute(); /* execute query */
$stmt
->store_result(); /* Store the result to get properties */
$num_of_rows
= $stmt->num_rows; /* Get the number of rows */
$stmt
->bind_result($id,$name); /* Bind the result to variables */
while ($stmt->fetch()) {
printf(
"%s %s", $id, $name);
}
$stmt
->close();
$mysqli
->close();
}
//end if