views:

39

answers:

1

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.

A: 

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.

michal kralik