w3resource

PHP mysqli: prepare() function

mysqli_prepare function / mysqli::prepare

The mysqli_prepare function / mysqli::prepare — Prepare an SQL statement for execution.

Syntax:

Object oriented style

mysqli_stmt mysqli::prepare ( string $query )

Procedural style

mysqli_stmt mysqli_prepare ( mysqli $link , string $query )

Parameter:

Name Description Required/Optional
link A link identifier returned by mysqli_connect() or mysqli_init() Required for procedural style only and Optional for Object oriented style
query The query, as a string. Required

Usage: Procedural style

mysqli_prepare ( string $query );

Return value:

mysqli_prepare() returns a statement object or FALSE if an error occurred.

Version: PHP 5, PHP 7

Example of object oriented style:

<?php
$mysqli = new mysqli("localhost", "user1", "datasoft123", "hr");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$city = "Mumbai";

/* create a prepared statement */
if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {

    /* bind parameters for markers */
    $stmt->bind_param("s", $city);

    /* execute query */
    $stmt->execute();

    /* bind result variables */
    $stmt->bind_result($district);

    /* fetch value */
    $stmt->fetch();

    printf("%s is in district %s\n", $city, $district);

    /* close statement */
    $stmt->close();
}

/* close connection */
$mysqli->close();
?>

Example of procedural style:

<?php
$link = mysqli_connect("localhost", "user123", "datasoft123", "hr");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$city = "Mumbai";

/* create a prepared statement */
if ($stmt = mysqli_prepare($link, "SELECT District FROM City WHERE Name=?")) {

    /* bind parameters for markers */
    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\n", $city, $district);

    /* close statement */
    mysqli_stmt_close($stmt);
}

/* close connection */
mysqli_close($link);
?>

Output:

Amersfoort is in district Utrecht
<?php
$con = new mysqli("localhost", "user1", "datasoft123", "hr");
$stmt = $con->prepare("SELECT * FROM employees WHERE LAST_NAME = ?");
$stmt->bind_param("s", $_POST['last_name']);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows === 0) exit('No rows');
while($row = $result->fetch_assoc()) {
  $id[] = $row['EMPLOYEE_ID'];
  $name[] = $row['FIRST_NAME'];
  $age[] = $row['LAST_NAME'];
}
$stmt->close();
?>

Output:

EMPLOYEE_ID  FIRST_NAME  LAST_NAME  EMAIL  PHONE_NUMBER       HIRE_DATE  JOB_ID   SALARY  COMMISSION_PCT  MANAGER_ID  DEPARTMENT_ID  
      156	 Janette King 	JKING  011.44.1345.429268 1987-08-12 SA_REP   10000.00 0.35           146          80 
      100 	 Steven King 	SKING  515.123.4567       1987-06-17 AD_PRES  24000.00 0.00             0          90 

See also

PHP Function Reference

Previous: poll
Next: query



Follow us on Facebook and Twitter for latest update.