mysql-php PDO slash causes error problems

Source: Internet
Author: User
Using PDO there is a problem, when the last character of the time to insert the database failed, it is difficult to understand, parameter binding how can the problem occur?

error_reporting(E_ALL);header("content-type:text/html;charset=utf8");$params = array();$params[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES UTF8';$dsn = "mysql:host=localhost;dbname=test;port=3306;";$dbh = new PDO($dsn, 'root', '123456', $params);$sql = 'INSERT INTO `fav_ls_1` (`uid`,`fid`,`type`,`title_md5`,`title`,`url`,`order`,`addtime`) VALUES (:uid,:fid,:type,:title_md5,:title,:url,:order,:addtime)';$bind = array(    ':uid' => 5919639,    ':fid' => 0,    ':type' => 1,    ':title_md5' => "0886c9605d1424e656c85736b4730e7f",    ':title' => '\\敌\\',    ':url' => "http://www.2345.com/?ie",    ':order' => 0,    ':addtime' => 1449156098,);$sth = $dbh->prepare($sql);if(false===$sth->execute($bind)){ print_r(  $sth->errorInfo () );}else{    echo  $dbh->lastInsertId();}

The error code is as follows:

Array ( [0] => 42000 [1] => 1064 [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'http://www.2345.com/?ie','0','1449156098')' at line 1 )

Add:
This setting is $dbh->setattribute (Pdo::attr_emulate_prepares, false); it can be run, but there are also inexplicable errors on other SQL.

Reply content:

Using PDO there is a problem, when the last character of the time to insert the database failed, it is difficult to understand, parameter binding how can the problem occur?

error_reporting(E_ALL);header("content-type:text/html;charset=utf8");$params = array();$params[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES UTF8';$dsn = "mysql:host=localhost;dbname=test;port=3306;";$dbh = new PDO($dsn, 'root', '123456', $params);$sql = 'INSERT INTO `fav_ls_1` (`uid`,`fid`,`type`,`title_md5`,`title`,`url`,`order`,`addtime`) VALUES (:uid,:fid,:type,:title_md5,:title,:url,:order,:addtime)';$bind = array(    ':uid' => 5919639,    ':fid' => 0,    ':type' => 1,    ':title_md5' => "0886c9605d1424e656c85736b4730e7f",    ':title' => '\\敌\\',    ':url' => "http://www.2345.com/?ie",    ':order' => 0,    ':addtime' => 1449156098,);$sth = $dbh->prepare($sql);if(false===$sth->execute($bind)){ print_r(  $sth->errorInfo () );}else{    echo  $dbh->lastInsertId();}

The error code is as follows:

Array ( [0] => 42000 [1] => 1064 [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'http://www.2345.com/?ie','0','1449156098')' at line 1 )

Add:
This setting is $dbh->setattribute (Pdo::attr_emulate_prepares, false); it can be run, but there are also inexplicable errors on other SQL.

Thanks to King's answers:
My problem is that the PHP environment is greater than 5.3.6, need to set Charset=utf8 in DSN, lower than PHP5.3.6 version of use set NAMES UTF8
Modify the code to resolve as follows:

Public function Connect ($config, $linkNum) {if (Empty ($this->_link[$linkNum])) {if (empty ($config)) $confi                g = $this->config;                $params = Array (); php<=5.3.6 Use this method to set the encoding if (!empty ($config [' Db_charset ']) && version_compare (php_version, ' 5.3.6 ', ' <= ') ) $params [Pdo::mysql_attr_init_command] = ' SET NAMES '.                $config [' Db_charset ']; Long link if (Isset ($config [' db_params '] [' pconnect ']) && $config [' db_params '] [' pconnect '] = = True) $                Params[pdo::attr_persistent] = true; @link http://zhangxugg-163-com.iteye.com/blog/1835721 5.3.6 under the bug if (Version_compare (php_version, ' 5.3.6 ', ' <= ' )) $params [pdo::attr_emulate_prepares] = false; Disable the impersonation preprocessing statement//php 5.3.6+ Specify the charset attribute in the PDO DSN $dsn _charset = Version_compare (php_version, ' 5.3.6 ', ' > ')?                "Charset={$config [' Db_charset ']};": "; $dsn = "{$config [' Db_type ']}:d Bname={$config [' db_name ']};host={$config [' Db_host ']};p ort={$config [' Db_port ']}; ".        $dsn _charset;        $this->dbname = $config [' db_name '];                $this->dbtype = $config [' Db_type '];            try {$this->_link[$linkNum] = new PDO ($DSN, $config [' Db_user '], $config [' Db_pass '], $params);            $this->_link[$linkNum]->setattribute (pdo::attr_case, pdo::case_lower);        $this->_link[$linkNum]->setattribute (Pdo::attr_default_fetch_mode, PDO::FETCH_ASSOC);        } catch (Pdoexception $e) {throw new CException ($e->getmessage ());    }} $this->_linkid = $this->_link[$linkNum]; return $this->_linkid;}

Note that setting the Attr_emulate_prepares to true does not really use the parameter binding function, which is just a simulation
The default is also false, do not understand why you have to open it

MySQL parameter bindings are implemented via the prepare statement, which should not be turned on if your database version is supported (MySQL version 4.1 or later)!

Your problem is probably because of this analog parameter binding, which is essentially an error (presumably, not verified) that is actually implemented by escaping

Please see
http://php.net/manual/zh/pdo.setattribute.php

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