php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40064 Wrong fieldnames in result (including table alias names)
Submitted: 2007-01-08 14:20 UTC Modified: 2007-01-08 14:44 UTC
From: christoph at ziegenberg dot de Assigned:
Status: Not a bug Package: PDO related
PHP Version: 5.2.0 OS: Windows XP SP2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: christoph at ziegenberg dot de
New email:
PHP Version: OS:

 

 [2007-01-08 14:20 UTC] christoph at ziegenberg dot de
Description:
------------
If you get the data of a query like "SELECT tablealias.fieldname FROM table tablealias.fieldname GROUP BY tablealias.fieldname" the result array contains not only the field name, but also the table alias name as field name.

Only using "SELECT tablealias.fieldname AS fieldname..." fixes this problem, but this should not be neccessary.

I don't know if this is a PDO or SQLite problem.

Reproduce code:
---------------
<?php
$sldb = new PDO('sqlite:memory');
$sldb->query("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT)");
$sldb->query("INSERT INTO test (id) VALUES (1)");

$queries = array();
$queries[] = "SELECT id FROM test";
$queries[] = "SELECT t1.id FROM test t1";
$queries[] = "SELECT t1.id FROM test t1 GROUP BY t1.id";
$queries[] = "SELECT t1.id AS id FROM test t1 GROUP BY t1.id";

foreach ($queries as $query)
{
    $stmt = $sldb->prepare($query);
    $stmt->execute();
    
    if ($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
    	echo 'result for query "' . $query . '":<br />';
    	print_r($row);
    	echo '<br /><br />';
    }
}
?>

Expected result:
----------------
result for query "SELECT id FROM test":
Array ( [id] => 1 )

result for query "SELECT t1.id FROM test t1":
Array ( [id] => 1 )

result for query "SELECT t1.id FROM test t1 GROUP BY t1.id":
Array ( [id] => 1 )

result for query "SELECT t1.id AS id FROM test t1 GROUP BY t1.id":
Array ( [id] => 1 ) 

Actual result:
--------------
result for query "SELECT id FROM test":
Array ( [id] => 1 )

result for query "SELECT t1.id FROM test t1":
Array ( [id] => 1 )

result for query "SELECT t1.id FROM test t1 GROUP BY t1.id":
Array ( [t1.id] => 1 )

result for query "SELECT t1.id AS id FROM test t1 GROUP BY t1.id":
Array ( [id] => 1 ) 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-01-08 14:44 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

That's what sqlite returns as field name, PDO/PHP don't change that but use what the library provides.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jul 23 19:01:29 2024 UTC