PHP Tutorial - PHP mysqli_stmt_init() Function






Definition

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

Syntax

PHP mysqli_stmt_init() Function has the following syntax.

mysqli_stmt_init(connection);

Parameter

  • connection - Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init()

Return

Returns an object.

Example


<?php//from ww  w .j  a v  a 2s.  c  o m
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno($con)){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$city="New York";
$stmt=mysqli_stmt_init($con);
if (mysqli_stmt_prepare($stmt,"SELECT District FROM City WHERE Name=?")){
  mysqli_stmt_bind_param($stmt,"s",$city);  // Bind parameters
  mysqli_stmt_execute($stmt);               // Execute query
  mysqli_stmt_bind_result($stmt,$district); // Bind result variables
  mysqli_stmt_fetch($stmt);                 // Fetch value
  print $district;
  mysqli_stmt_close($stmt);
}

mysqli_close($con);
?>