PHP mysqli_prepare() function prepares an SQL statement for execution.
Syntax
object mysqli_prepare(connection, query);
Parameter | Description |
---|---|
connection : | Required parameter. The MySQL connection to be used |
query : | Required parameter. The query, as a string. |
Return Values : | Returns a statement object or FALSE if an error occurred. |
<?php $con = mysqli_connect("localhost","user","password","db"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $name = "Michael"; // prepare statement if ($stmt = $con->prepare("SELECT fname FROM employee WHERE fname = ?")) { // bind parameters for markers $stmt->bind_param("s", $name ); // execute query $stmt->execute(); // bind result variables $stmt->bind_result($designation); // fetch value $stmt->fetch(); echo $name . " working as ". $designation; // close statement $stmt->close(); } mysqli_close($con); ?>