PHP MySQLI Prep Statement


 

PHP MySQLI Prep Statement

In the current tutorial we will study how to create prepared statement in mysql and how to retrieve values using mysqli::prepare, mysqli::prepare - Prepares a SQL query and returns a statement handle to be used for further operations on the statement. The query should consist of a single sql query.

In the current tutorial we will study how to create prepared statement in mysql and how to retrieve values using mysqli::prepare, mysqli::prepare - Prepares a SQL query and returns a statement handle to be used for further operations on the statement. The query should consist of a single sql query.

PHP-MySQLI:Prepared-Statement

mysqli::prepare - Prepares a SQL query and returns a statement handle to be used for further operations on the statement. The query should consist of a single sql query.

There are two styles are available in this current statement:

  1. Object oriented style
  2. Procedural style

In the following example we use Object Oriented Style. The format of this style is:

mysqli_stmt mysqli::prepare (string $query)

Table is as follows:

city_id city_name
1 new delhi
2 kolkata
3 mumbai
4 chennai
5 ahmedabad
6 ajmer

PHP Mysqli Prepares Example:

<?php

$mysqli=new mysqli("localhost","root","","ajax");

if(mysqli_connect_errno()){

printf("connection failed:%f\n", mysql_connect_error());

exit();}

$city="ahmedabad";

if($stmt=$mysqli->prepare("select city_id from city where city_name=?")){

$stmt->bind_param("s",$city);

$stmt->execute();

$stmt->bind_result($cityid);

$stmt->fetch();

printf("%s",$cityid);

$stmt->close();}

$mysqli->close();

?>

 

Output:

5

Ads