php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47710 Undefined Index when accessing results by array
Submitted: 2009-03-18 20:41 UTC Modified: 2009-05-08 01:00 UTC
Votes:18
Avg. Score:4.1 ± 0.8
Reproduced:17 of 17 (100.0%)
Same Version:10 (58.8%)
Same OS:7 (41.2%)
From: grant at coolhandjob dot com Assigned:
Status: No Feedback Package: PDO related
PHP Version: 5.3, 6CVS (2009-03-18) OS: CentOS 5.2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: grant at coolhandjob dot com
New email:
PHP Version: OS:

 

 [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 ) 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-04-30 10:54 UTC] jani@php.net
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();
}
?>
 [2009-04-30 18:21 UTC] grant at coolhandjob dot com
It returns:

Array ( [id] => 2 [0] => 2 [name] => cod [1] => cod ) Array ( [id] => 1 [0] => 1 [name] => mohaa [1] => mohaa ) 

I can access $row[0] etc just fine but not $row["id"] or $row["name"] as thats when I get undefined index error.
 [2009-05-08 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jun 28 15:01:34 2025 UTC