PHP extension PDO MySQL Pdostatement::bindparam vs Bindvalue

Source: Internet
Author: User
Tags rowcount stmt vps

PHP extension PDO MySQL Pdostatement::bindparam vs BindvalueTechnologyMaybe yes posted on 2015-11-16 13:27 original link: http://blog.lmlphp.com/archives/155/The_difference_of_PDOStatement_bindParam_ And_bindvalue_of_php_extension_pdo_mysql from: lmlphp backyard

The previous days will lblog online experience site http://lblog.lmlphp.com/relocated to the VPS, in fact, has been in the past for several months. PHP on the new VPS version is relatively high, so run the time will be prompted by the MySQL series function outdated Deprecated error. The simplest way to do this is to block out the error level, but this is not my style, and I prefer to do it in a better way. But there is no time to work at all, write a mysqlpdo Enhance class, according to the previous lmlphp in the style of the Mysql driver class. To this end, write the PDO operation class is also specifically defined interface to constrain their behavior, for fear of problems, after testing proved that fully compatible with the Mysql class before, after spending so many months of time, really hurt.

This time to write the PDO driver class, did not refer to the other people's writing, completely look at official documents, combined with their own needs, as far as possible simple implementation. In fact, PDO is already an object-oriented style, actually does not need what driver class to too many packages, write this just for better compatibility in the project code. When I first looked at Bindvalue and Bindparam, the document gave me the feeling that it was just a variable, and the exact value. Until later found that one is a reference, and one is a common parameter. When testing the modification operation, it was found that the last field in the database was the same as the previous string, the int type did not receive the impact, perhaps the brain was too tired, the problem was a long time, the second genius to figure out because in the loop when the use of bindparam caused.

The mysqlpdoenhance driver class has been uploaded to Lmlphp and Lblog, and Lblog has automatically selected the corresponding driver class when initializing the instance. This improvement makes the Lblog system more excellent and adaptable to the server environment.

The Mysqlpdoenhance class continues the simple style of the Mysql class, with only a heavyweight query method that automatically determines whether to return a resource or affect the number of rows. It is also found that it is not a good practice to do this when using PDO. Because the number of rows that are affected when a select is executed, this is not the same as common sense, and the general knowledge of the database should know that SELECT does not affect the row, but the value returned by the RowCount method in PDO is the selected number of rows. So, for this reason, only from the SQL above a simple judgment, is not very perfect.

Attached to the query method excerpt.

0123456789101112131415161718192021222324252627publicfunctionQuery($sql, $params = Array()){ $stmt = $this->db->prepare($sql, Array(Pdo::mysql_attr_use_buffered_query=> True));if($params){Foreach($paramsAs$k =>$v){ if(Is_Array($v)){ $value = $v[' value '];$type =Isset($v[' type '])?$v[' type ']: false;$length =Isset($v[' Length '])?$v[' Length ']: false;if($type&&$length){ $stmt->bindvalue($k, $value, $type, $length);}Elseif($type){ $stmt->bindvalue($k, $value, $type);}Else{ $stmt->bindvalue($k, $value);} }Else{ $stmt->bindvalue($k, $v);} } } $stmt->execute();if(Preg_match('/^update|^insert/i ',Trim($sql))){ return $stmt->rowcount();}Else{ return $stmt->fetchall(Pdo::fetch_assoc);}}Read (607) reviews (0) View comments

PHP extension PDO MySQL Pdostatement::bindparam vs Bindvalue

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.