Tural Ali
Tural Ali

Reputation: 23240

Need some explanation about prepared statements in PHP

Reading this page http://www.php.net/manual/en/mysqli-stmt.bind-result.php

I got some questions about PS

Upvotes: 2

Views: 172

Answers (1)

David Barker
David Barker

Reputation: 14620

As mysqli_prepare() creates an object with the link identifier and the query you want to run, you don't need to use mysqli_stmt::init.

The advantage of using mysqli_stmt::init is to create an instance of the object in a config file for example. Any subsequently required / included scripts will have a query object ready for use, reducing the amount of code you need to write.

Second question: Yes. Think of the object as having a lifecycle from the line of code it is initialised to the last line of code of the script OR when its __destruct() magic method is called. It can be used at anytime once created allowing you to constantly change the query and execute more code with the same object.

As a note: you must always use mysqli_stmt::close after each query (once you have taken the results). This clears the objects query and result sets, re-initialising the object to the same state it would have been in when first created.

Hope that helps.

Upvotes: 4

Related Questions