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

  • In examples i don't see $stmt = $db->stmt_init(); Is it required to create new instance ( add $stmt=$db->stmt_init() line) at the beginning of the every statement and $stmt->close at the end or it doesn't matter at all: I can start from $stmt = $mysqli->prepare("... ??
  • Can I create statement object 1 time at the beginning of the code and use it all code long?

  • 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.

    链接地址: http://www.djcxy.com/p/44878.html

    上一篇: mySQLi准备好的语句无法获取

    下一篇: 在PHP中需要一些关于准备语句的解释