Latest web development tutorials
 

PHP mysqli_stmt_init() Function

< PHP MySQLi Reference

Example

Initialize a statement and return an object to use with mysqli_stmt_prepare():

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$city="Sandnes";

// Create a prepared statement
$stmt=mysqli_stmt_init($con);

if (mysqli_stmt_prepare($stmt,"SELECT District FROM City 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);
?>

Definition and Usage

The mysqli_stmt_init() function initializes a statement and returns an object suitable for mysqli_stmt_prepare().


Syntax

mysqli_stmt_init(connection);

Parameter Description
connection Required. Specifies the MySQL connection to use

Technical Details

Return Value: Returns an object
PHP Version: 5+

< PHP MySQLi Reference