Home Tutorial Php Phpdatabase PHP MySQLI Prep Statement

 
 

PHP MySQLI Prep Statement
Posted on: February 17, 2010 at 12:00 AM
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

Related Tags for PHP MySQLI Prep Statement:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.