MYSQL PDO返回类型奇怪的转换
我在 MYSQL 中有以下表定义
CREATE TABLE IF NOT EXISTS `test_cases` (
`id` int(10) unsigned NOT NULL auto_increment,
`exercise_id` int(10) unsigned NOT NULL,
`author_id` int(10) unsigned NOT NULL,
`input_1_value` varchar(255) default NULL,
`input_2_value` varchar(255) default NULL,
`input_3_value` varchar(255) default NULL,
`input_4_value` varchar(255) default NULL,
`input_5_value` varchar(255) default NULL,
`output_value` varchar(255) default NULL,
PRIMARY KEY (`id`),
KEY `test_cases_ibfk_1` (`exercise_id`),
KEY `author_id` (`author_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3218 ;
我在该表中有以下条目
INSERT INTO `test_cases` (`id`, `exercise_id`, `author_id`, `input_1_value`, `input_2_value`, `input_3_value`, `input_4_value`, `input_5_value`, `output_value`) VALUES
(560, 145, 496, '0', NULL, NULL, NULL, NULL, '0')
我有以下查询来从我感兴趣的表中获取上面的行
SELECT id, exercise_id, author_id, input_1_value, input_2_value, input_3_value, input_4_value, input_5_value, output_value FROM test_cases WHERE exercise_id=145
,但在 input_1_value
中的值存在问题>。在 phpMyAdmin 中运行查询将返回值“0”,这是预期的。然而,运行以下 php 脚本返回的值为“true”,这完全是左字段,让我和我的项目主管感到困惑。 php 脚本如下...
$db = DBCxn::getCxn();
$sql = "SELECT id, exercise_id, author_id, input_1_value, input_2_value, input_3_value, input_4_value, input_5_value, output_value FROM test_cases WHERE exercise_id=:exid";
$st=$db->prepare($sql);
$st->bindParam(":exid", $exerciseId, PDO::PARAM_INT); // $exerciseID == 145
$st->execute();
$row = $st->fetch(); // default fetch mode is PDO::FETCH_BOTH
echo $row['input_1_value'];
这回显“true”!!!!为什么???为什么它不打印“0”???
使用 print_r($row);
获取更多信息,我得到以下输出
Array ( [id] => 560 [0] => 560 [exercise_id] => 145 [1] => 145 [author_id] => 496 [2] => 496 [input_1_value] => true [3] => true [input_2_value] => [4] => [input_3_value] => [5] => [input_4_value] => [6] => [input_5_value] => [7] => [output_value] => true [8] => true )
注意 output_value
当它应该为“0”时也返回为“true”。
有谁知道这是怎么回事?任何帮助表示赞赏。非常感谢。
I have the following table definition in MYSQL
CREATE TABLE IF NOT EXISTS `test_cases` (
`id` int(10) unsigned NOT NULL auto_increment,
`exercise_id` int(10) unsigned NOT NULL,
`author_id` int(10) unsigned NOT NULL,
`input_1_value` varchar(255) default NULL,
`input_2_value` varchar(255) default NULL,
`input_3_value` varchar(255) default NULL,
`input_4_value` varchar(255) default NULL,
`input_5_value` varchar(255) default NULL,
`output_value` varchar(255) default NULL,
PRIMARY KEY (`id`),
KEY `test_cases_ibfk_1` (`exercise_id`),
KEY `author_id` (`author_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3218 ;
I have the following entry into this table
INSERT INTO `test_cases` (`id`, `exercise_id`, `author_id`, `input_1_value`, `input_2_value`, `input_3_value`, `input_4_value`, `input_5_value`, `output_value`) VALUES
(560, 145, 496, '0', NULL, NULL, NULL, NULL, '0')
I have the following query to get the above row from the table
SELECT id, exercise_id, author_id, input_1_value, input_2_value, input_3_value, input_4_value, input_5_value, output_value FROM test_cases WHERE exercise_id=145
I'm interested in, and having problems with, the value in input_1_value
. Running the query in phpMyAdmin will return the value as '0', which is as expected. However running the following php script returns the value as 'true' which is completely left field and leaving me and my project supervisor stumped. The php script is below...
$db = DBCxn::getCxn();
$sql = "SELECT id, exercise_id, author_id, input_1_value, input_2_value, input_3_value, input_4_value, input_5_value, output_value FROM test_cases WHERE exercise_id=:exid";
$st=$db->prepare($sql);
$st->bindParam(":exid", $exerciseId, PDO::PARAM_INT); // $exerciseID == 145
$st->execute();
$row = $st->fetch(); // default fetch mode is PDO::FETCH_BOTH
echo $row['input_1_value'];
this echos 'true'!!!! why??? why does it not print '0'???
for even more information using print_r($row);
I get the following output
Array ( [id] => 560 [0] => 560 [exercise_id] => 145 [1] => 145 [author_id] => 496 [2] => 496 [input_1_value] => true [3] => true [input_2_value] => [4] => [input_3_value] => [5] => [input_4_value] => [6] => [input_5_value] => [7] => [output_value] => true [8] => true )
Note output_value
is also returned as 'true' when it should be '0'.
Does anyone know what is going on here? Any help is appreciated. GREATLY appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将常量
PDO::ATTR_STRINGIFY_FETCHES
设置为 true。查看 http://php.net/manual/en/pdo.setattribute。 php,看看是否有帮助。
Try setting the constant
PDO::ATTR_STRINGIFY_FETCHES
to true.Take a look at http://php.net/manual/en/pdo.setattribute.php, see if it helps.