Prepared Statements

Prepared Statements

Prepare Statements are used to create the queries more secure and give better performance.

There are 2 types of prepared statement
1. Executes data manipulation statemenet
2. executes data retrieval statements

Example:
$con = mysqli_connect($host,$uname,$pass,$dbname)

$sql = ‘INSERT INTO user VALUES(?, ?)’;
$stmt = mysqli_stmt_init($con);

if (mysqli_stmt_prepare($stmt, $sql)) {
mysqli_stmt_bind_param($stmt, ‘is’, $name, $age);

$nam = “test”;
$age = ‘23′;
mysqli_stmt_execute($stmt);

}

In the above code

1.mysqli_stmt_init($con)

Above line will initialiaze a statement.

2.After initialize the statement we have to bind the varibles to the statement. In the above code following line will to that thing.
mysqli_stmt_bind_param($stmt, ‘is’, $name, $age);

3. Then we have to execute the query. In the above code following line will to that thing.
mysqli_stmt_execute($stmt)

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.