PHP mysqli_prepare() Function

PHP mysqli_prepare() function prepares an SQL statement for execution.

Syntax

object mysqli_prepare(connection, query);

mysqli_prepare() Function Parameter

ParameterDescription
connection :Required parameter. The MySQL connection to be used
query :Required parameter. The query, as a string.

mysqli_prepare() Function Return Value

Return Values :Returns a statement object or FALSE if an error occurred.

mysqli_prepare() Function Example

<?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);
?>