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



PHP: Tips of the Day

var_export(): var_export() dumps a PHP parseable representation of the item.

You can pass true as the second parameter to return the contents into a variable.

Example:

<?php
$myarray = [ "PHP", "Tips" ];
$mystring = "PHP Tips";
$myint = 28;

var_export($myarray);
var_export($mystring);
var_export($myint);
?>

Output:

array (
  0 => 'PHP',
  1 => 'Tips',
)'PHP Tips'28

To put the content into a variable, you can do this:

$array_export = var_export($myarray, true);
$string_export = var_export($mystring, true);
$int_export = var_export($myint, 1); // any `Truthy` value

After that, you can output it like this:

printf('$myarray = %s; %s', $array_export, PHP_EOL);
printf('$mystring = %s; %s', $string_export, PHP_EOL);
printf('$myint = %s; %s', $int_export, PHP_EOL);

Example:

<?php
$myarray = [ "PHP", "Tips" ];
$mystring = "PHP Tips";
$myint = 28;
$array_export = var_export($myarray, true);
$string_export = var_export($mystring, true);
$int_export = var_export($myint, 1);
printf('$myarray = %s; %s', $array_export, PHP_EOL);
printf('$mystring = %s; %s', $string_export, PHP_EOL);
printf('$myint = %s; %s', $int_export, PHP_EOL);
?>

This will produce the following output:

Output:

$myarray = array (
  0 => 'PHP',
  1 => 'Tips',
);
$mystring = 'PHP Tips';
$myint = 28;