Initialize a statement and return an object to use with mysqli_stmt_prepare() in PHP
Description
The following code shows how to initialize a statement and return an object to use with mysqli_stmt_prepare().
Example
/*from ww w.j a v a 2s .com*/
<?php
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$city="NewName";
// Create a prepared statement
$stmt=mysqli_stmt_init($con);
if (mysqli_stmt_prepare($stmt,"SELECT name FROM employee WHERE Name=?")){
// Bind parameters
mysqli_stmt_bind_param($stmt,"s",$city);
// Execute query
mysqli_stmt_execute($stmt);
// Bind result variables
mysqli_stmt_bind_result($stmt,$district);
// Fetch value
mysqli_stmt_fetch($stmt);
printf("%s is in district %s",$city,$district);
// Close statement
mysqli_stmt_close($stmt);
}
mysqli_close($con);
?>
The code above generates the following result.