使用PDO出现了一个问题,当字符的最后一个为\的时候插入数据库失败,很是费解,参数绑定怎么会出现如此的问题?
<code>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(); }</code>
错误代码如下:
<code>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 )</code>
补充一下:
这个设置了$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);就可以运行了,但是在其他sql上也会出现莫名其妙的错误。
回复内容:
使用PDO出现了一个问题,当字符的最后一个为\的时候插入数据库失败,很是费解,参数绑定怎么会出现如此的问题?
<code>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(); }</code>
错误代码如下:
<code>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 )</code>
补充一下:
这个设置了$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);就可以运行了,但是在其他sql上也会出现莫名其妙的错误。
感谢king同学的答案:
我的问题是PHP环境是大于5.3.6的,需要在dsn中设置charset=utf8,低于PHP5.3.6版本的使用SET NAMES UTF8
修改代码如下解决:
<code>public function connect($config, $linkNum) { if(empty($this->_link[$linkNum])) { if(empty($config)) $config = $this->config; $params = array(); // PHP') ? "charset={$config['db_charset']};" : ''; $dsn = "{$config['db_type']}:dbname={$config['db_name']};host={$config['db_host']};port={$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; }</code>
注意,把ATTR_EMULATE_PREPARES设置为true并没有真正的用参数绑定功能,这只是模拟
本来默认也是false,不明白为何你要开启它
MySQL的参数绑定是通过prepare语句实现的,如果你的数据库版本支持(MySQL 4.1以上版本支持),不应该开启这个选项!
你的问题很可能是因为这个模拟参数绑定,也就是本质上其实是通过转义实现的出现的错误(推测,未验证)
请见
http://php.net/manual/zh/pdo.setattribute.php

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

tomakephpapplicationsfaster,关注台词:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

到ImprovephPapplicationspeed,关注台词:1)启用opcodeCachingwithapCutoredUcescriptexecutiontime.2)实现databasequerycachingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandredececonnection.4 limitsclection.4.4

依赖注入(DI)通过显式传递依赖关系,显着提升了PHP代码的可测试性。 1)DI解耦类与具体实现,使测试和维护更灵活。 2)三种类型中,构造函数注入明确表达依赖,保持状态一致。 3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

databasequeryOptimizationinphpinvolVolVOLVESEVERSEVERSTRATEMIESOENHANCEPERANCE.1)SELECTONLYNLYNESSERSAYCOLUMNSTORMONTOUMTOUNSOUDSATATATATATATATATATATRANSFER.3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver Mac版
视觉化网页开发工具

Dreamweaver CS6
视觉化网页开发工具

SublimeText3汉化版
中文版,非常好用