php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #61758 mysql_fetch_field() incorrectly reports table name when using a table alias
Submitted: 2012-04-17 19:03 UTC Modified: 2012-06-01 06:46 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: johnbillion at gmail dot com Assigned: philip (profile)
Status: Closed Package: MySQL related
PHP Version: 5.3.10 OS: Windows 7
Private report: No CVE-ID: None
 [2012-04-17 19:03 UTC] johnbillion at gmail dot com
Description:
------------
The value of the 'Table' property returned by mysql_fetch_field() will be incorrect if a table alias was used in the SQL query.

Test script:
---------------
// It's assumed a MySQL resource has been established here

$result = mysql_query( 'SELECT * FROM table AS t' );
$i = 0;
while ( $i < mysql_num_fields( $result ) ) {
	$col_info[] = mysql_fetch_field( $result );
	$i++;
}
echo '<pre>' . print_r( $col_info, true ) . '</pre>';

Expected result:
----------------
The 'Table' property of the object output should be 'table'.

Actual result:
--------------
The 'Table' property of the object output is 't'.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-04-18 03:27 UTC] phpmpan at mpan dot pl
You have introduced an alias for a table in your query and the result contents reflects this. There is a good reason for such behaviour. Consider a following query:

SELECT *
FROM SomeTable AS workers
JOIN SomeTable AS managers ON managers.id = workers.boss
 [2012-04-18 10:53 UTC] johannes@php.net
Please use mysqli and mysqli_fetch_field() instead of the old mysql extension. The old MySQL extension only receives bug fixes no new "features". mysqli will report two fields, "table", which has the aliased name, and "origtable", which has the original name.
 [2012-04-18 10:53 UTC] johannes@php.net
-Status: Open +Status: Wont fix
 [2012-04-18 17:51 UTC] philip@php.net
Marking as a doc bug for now (as suggested by Johannes), so I won't forget.
 [2012-04-18 17:51 UTC] philip@php.net
-Status: Wont fix +Status: Re-Opened -Type: Bug +Type: Documentation Problem -Assigned To: +Assigned To: philip
 [2012-06-01 06:46 UTC] philip@php.net
Automatic comment from SVN on behalf of philip
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=325919
Log: Documented PHP bug #61758 -- will report the alias name
 [2012-06-01 06:46 UTC] philip@php.net
-Status: Re-Opened +Status: Closed
 [2012-06-01 06:46 UTC] philip@php.net
Fixed in SVN, thank you for the report.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 08 20:01:36 2024 UTC