|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-03-18 20:41 UTC] grant at coolhandjob dot com
Description:
------------
When trying to access the returned associative array I get a undefined index error. Works as expected in PHP5.2.9 but have the same error with the latest PHP5.3.
Reproduce code:
---------------
<?php
try {
$pdo = new PDO("mysql:host=localhost;dbname=dbname;", "dbuser", "password");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT id, name FROM games ORDER BY name';
foreach ($pdo->query($sql) as $row) {
print $row['id'] . " - ";
print $row['name'] . "<br />";
}
print_r($row);
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
Expected result:
----------------
2 - cod
1 - mohaa
Array ( [id] => 1 [0] => 1 [name] => mohaa [1] => mohaa )
Actual result:
--------------
Notice: Undefined index: id in /var/www/html/temp.php on line 9
-
Notice: Undefined index: name in /var/www/html/temp.php on line 10
Notice: Undefined index: id in /var/www/html/temp.php on line 9
-
Notice: Undefined index: name in /var/www/html/temp.php on line 10
Array ( [id] => 1 [0] => 1 [name] => mohaa [1] => mohaa )
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 15:00:01 2025 UTC |
What does this script output: <?php try { $pdo = new PDO("mysql:host=localhost;dbname=dbname;", "dbuser", "password"); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = 'SELECT id, name FROM games ORDER BY name'; foreach ($pdo->query($sql) as $row) { print_r($row); } } catch(PDOException $e) { echo $e->getMessage(); } ?>