PHP mysqli preprocessing how to bind parameters

Source: Internet
Author: User
Tags rowcount
/** * PHP preprocessing Execute SQL * $sql [string]sql statement * $args [array] parameter */public function exesql ($sql, $args) {$mysqli _stmt= $mysqli Prepare ($sql);//Because $sql is passed in by the caller, the number of SQL statements and arguments is indeterminate//question 1: How do I get the parameter type? Is there a corresponding function in PHP? If not I use the following method: Getparamtypestr ($arr) is it feasible? Do you have any good advice? Question 2: How to bind parameters? The following is the binding method when the number of arguments is determined. $mysqli _stmt->bind_param ("SSI", "xx", "xx"), $mysqli _stmt->execute (); $mysqli->close ();}   Private Function Getparamtypestr ($arr) {$count = count ($arr); $typestr = ""; for ($i = 0; $i < $count; $i + +) {$type = GetType ($arr [$i]); switch ($type) {case ' integer ': $typestr. = "I"; Break;case "float": Case "Double": $ typestr.= "D"; Break;case "string": $typestr. = "S"; break;}} return $TYPESTR;}


I know that Java is implemented in the following ways:
Preprocessing in Java execution sqlpublic void Exesql (String sql,object[] args) {PreparedStatement PreparedStatement =        Connection.preparestatement (SQL); for (int i =0;i


Which friend to help answer the above 2 questions, I just turned to PHP, read the document, there is a mention of reflection, not very understand, there are friends said by replacing the '? ' in SQL, but also look at friends detailed guidance, it is best to provide a point core code. Thank you so much!


Reply to discussion (solution)

2.

$callback = Array ($mysqli _stmt, ' Bind_param ');//The parameter type description is added to the array array_unshift ($args, Getparamtypestr ($args)); Call_user_func_array ($callback, $args);//Its invocation is similar to: $mysqli _stmt->bind_param (Getparamtypestr ($args), $args [0], $ ARGS[1], $args [2] ...);

Recommend you use pdo,mysqli this function is not good, PDO of the Bindparam () method to be more intuitive
http://www.php.net/manual/en/pdostatement.bindparam.php

I have one of my own PHP PDO classes that you can use directly:

  dbhost= $dbhost, $this->dbuser= $dbuser, $this->dbpwd= $dbpwd; $this->dbname= $dbname; $this->dbport=$ Dbport; $this->ifpdo= $ifpdo; $this->dburi= $dburi;//pdo uri parameter, you can check the manual if ($this->ifpdo==1) {///means call PDO to manipulate the database $ this->handler= $this->createpdo ();} ElseIf ($this->ifpdo==0) {//here can write Mysqli method $this->handler=null;}} /*----------------Here is the entry---------------------*///@param sql: Full SQL statement passed on external call//@param bindarray: An array of parameters for the binding, related to the SQL statement, If there is no PDO placeholder here is a null//@param action: Pass Action arguments, "select"/"Update"/"delete"/"Insert" public function Exesql ($sql, $bindArray = Array (), $action = "") {$stmt = $this->handler->prepare ($sql); $stmt->execute ($bindArray); switch ($action) { Case "SELECT": Return $stmt->fetch (PDO::FETCH_ASSOC), break;case "SelectAll": Return $stmt->fetchall (pdo::fetch _ASSOC); break;case "Update": Case "Delete": Return $stmt->rowcount (); Break;case "Insert": Return $this->handler- >lastinsertid (); Break;case "Count": Return $stmt->rowcount ();d Efault:return "";}} Public Function Query ($sql) {RETUrn $this->handler->query ($sql);} Private Function Createpdo () {try{$handler =new PDO ($this->dburi, $this->dbuser, $this->dbpwd); $handler SetAttribute (pdo::attr_errmode,pdo::errmode_exception); return $handler;} catch (Pdoexception $e) {$e->getmessage (); $this->handler=null;}} Private Function __get ($args) {if ($args = = ' Handler ') {return $this->handler;}}} Require (neo_a_p. ' \data\sqlconfig.php ');//Here is the SQL connection file, the following object is created when the variable is required to have $handler=new Mysqlconnect ($dbhost, $dbuser, $dbpwd, $dbname, $dbport, $ifpdo, $dburi);? >

I have one of my own PHP PDO classes that you can use directly:

PHP code? 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869707172737475767778 ...

When called, the direct require contains the class, and then the $handler is the Action object

Thanks for the 2 enthusiastic friends upstairs.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.