简体   繁体   English

PHP版本之间的不同PDO行为

[英]Different PDO behaviours between PHP versions

I have three servers running as my development, staging and production servers. 我有运行中的三台服务器作为开发,登台和生产服务器。 The PHP version running are: 5.5.23 , 5.5.9 , 5.5.20 respectively. PHP版本运行是: 5.5.235.5.95.5.20分别。

They are running exactly the same code. 他们正在运行完全相同的代码。 But my development server was throwing exceptions whereas the other two works fine. 但是我的开发服务器抛出了异常,而其他两个运行良好。 The exception is follow: 例外如下:

Error: [PDOException] SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'comment_id' at row 1 错误:[PDOException] SQLSTATE [HY000]:常规错误:1366错误的整数值:“第1行的'comment_id'列的''
Request URL: /comments/add.json Stack Trace: 0 /Users/me/Sites/MySite/lib/Cake/Model/Datasource/DboSource.php(460): PDOStatement->execute(Array)1 请求URL:/comments/add.json堆栈跟踪:0 /Users/me/Sites/MySite/lib/Cake/Model/Datasource/DboSource.php(460):PDOStatement-> execute(Array)1
1 /Users/me/Sites/MySite/lib/Cake/Model/Datasource/DboSource.php(426): DboSource->_execute('INSERT INTO `po...', Array)1 1 /用户/我/站点/ MySite / lib /蛋糕/模型/数据源/DboSource.php(426):DboSource-> _ execute('INSERT INTO`po ...',Array)1
2 /Users/me/Sites/MySite/lib/Cake/Model/Datasource/DboSource.php(1008): DboSource->execute('INSERT INTO `po...')1 2 /Users/me/Sites/MySite/lib/Cake/Model/Datasource/DboSource.php(1008):DboSource-> execute('INSERT INTO`po ...')1
3 /Users/me/Sites/MySite/lib/Cake/Model/Model.php(1744): DboSource->create(Object(Notification), Array, Array)1 3 /Users/me/Sites/MySite/lib/Cake/Model/Model.php(1744):DboSource-> create(Object(Notification),Array,Array)1
4 /Users/me/Sites/MySite/app/Controller/CommentsController.php(181): Model->save(Array)1 4 /Users/me/Sites/MySite/app/Controller/CommentsController.php(181):模型->保存(数组)1
5 [internal function]: CommentsController->add() 5 [内部功能]:CommentsController-> add()

The value being inserted is a bool(false) while the database is expecting integers. 当数据库需要整数时,插入的值是bool(false) So I had to do something like to handle this issue for my development server: 因此,我必须为我的开发服务器做类似处理此问题的操作:

//The comment id is set to false. Needs to turn it to NULL as DB can't handle false for integer
if($this->Comment->id === false)
    $notification['Notification']['comment_id'] = NULL;
else
    $notification['Notification']['comment_id'] = $this->Comment->id;

My question is: Why my staging and production server can handle this issue (auto-convert the false value into integer, possibly), but my development server cannot? 我的问题是:为什么我的登台服务器和生产服务器可以处理此问题(可能将错误值自动转换为整数),但是我的开发服务器不能?

As you can see, my servers are running CakePHP . 如您所见,我的服务器正在运行CakePHP I have been looking at the PDO versions but it seemed like it doesn't give me too much insight. 我一直在研究PDO版本,但似乎并没有给我太多的了解。 My development server is running on my Mac Book Mac OS X 10.10. 我的开发服务器正在Mac Book Mac OS X 10.10上运行。 The staging and production are running Ubuntu 12.04. 过渡和生产正在运行Ubuntu 12.04。

You most likely have strict mode for MySQL disabled on your Production and Staging environments, while your development environment has this enabled. 在生产和暂存环境中,很可能已禁用MySQL的严格模式,而开发环境已启用了该模式。

See more here : 在这里查看更多

Strict mode controls how MySQL handles invalid or missing values in data-change statements such as INSERT or UPDATE. 严格模式控制MySQL如何处理数据更改语句(例如INSERT或UPDATE)中的无效或缺失值。 A value can be invalid for several reasons. 值可能由于多种原因而无效。 For example, it might have the wrong data type for the column, or it might be out of range. 例如,它可能具有错误的列数据类型,或者可能超出范围。 A value is missing when a new row to be inserted does not contain a value for a non-NULL column that has no explicit DEFAULT clause in its definition. 当要插入的新行不包含其定义中没有显式DEFAULT子句的非NULL列的值时,缺少值。 (For a NULL column, NULL is inserted if the value is missing.) (对于NULL列,如果缺少该值,则插入NULL。)

If strict mode is not in effect, MySQL inserts adjusted values for invalid or missing values and produces warnings 如果严格模式无效,则MySQL会为无效或丢失的值插入调整后的值并产生警告

You can read on how to change this mode here . 您可以在此处阅读如何更改此模式

On OS X, the following link has more details : 在OS X上, 以下链接具有更多详细信息

After a lot of poking around I found out that MySQL for OS X from Oracle ships with a /usr/local/mysql/my.cnf which is loaded on startup. 经过一番摸索,我发现来自Oracle的MySQL for OS X附带了/usr/local/mysql/my.cnf,它在启动时加载。 In this file is a sole configuration directive for sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES. 在此文件中,是sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES的唯一配置指令。 Once I commented this out and restarted the server strict mode was off, my ORM worked and I was happy. 一旦我注释掉并重新启动了服务器严格模式,我的ORM便开始工作了,我感到很高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM