Current Location: Home> Function Categories> mysqli::stmt_init

mysqli::stmt_init

(mysqli_stmt_init) Initialize a statement and return an object used with mysqli_stmt_prepare
Name:mysqli::stmt_init
Category:MySQLi
Programming Language:php
One-line Description:Initialize a statement and return an object for use with stmt_prepare().

Definition and usage

stmt_init() / mysqli_stmt_init() function initializes the statement and returns an object suitable for mysqli_stmt_prepare() use.

Example

Example 1 - Object-Oriented Style

Initialize a statement and return an object for stmt_prepare() :

 <?php
$mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( $mysqli -> connect_errno ) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error ;
  exit ( ) ;
}

$city = "Sandnes" ;

// Create a preprocessing statement
$stmt = $mysqli -> stmt_init ( ) ;

if ( $stmt -> prepare ( "SELECT District FROM City WHERE Name=?" ) ) {
  // Bind parameters
  $stmt -> bind_param ( "s" , $city ) ;

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

  // Bind the result variable
  $stmt -> bind_result ( $district ) ;

  // Get the value
  $stmt -> fetch ( ) ;

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

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

$mysqli -> close ( ) ;
?>

Example 2 - Procedural Style

Initialize a statement and return an object for mysqli_stmt_prepare():

 <?php
$con = mysqli_connect ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( mysqli_connect_errno ( ) ) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error ( ) ;
  exit ;
}

$city = "Sandnes" ;

// Create a preprocessing 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 the result variable
  mysqli_stmt_bind_result ( $stmt , $district ) ;

  // Get the value
  mysqli_stmt_fetch ( $stmt ) ;

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

  // Close statement
  mysqli_stmt_close ( $stmt ) ;
}

mysqli_close ( $con ) ;
?>
Similar Functions
Popular Articles