mysql_field_table

(PHP 3, PHP 4, PHP 5)

mysql_field_table --  取得指定字段所在的表名

说明

string mysql_field_table ( resource result, int field_offset )

返回指定字段所在的表的名字。

为向下兼容仍然可以使用 mysql_fieldtable(),但反对这样做。


add a note add a note User Contributed Notes
spam at blondella dot de
03-Oct-2006 07:09
<?php
/*
this function might help in the case described above :-)
*/
function mysql_field_table_resolve_alias($inQuery,$inResult,$inFieldName) {
  
$theNameOrAlias = mysql_field_table($inResult,$inFieldName);
  
//check, if AS syntax is being used
  
if(ereg(" AS ",$inQuery))  {
    
//catch words in query
    
$theWords = explode(" ",ereg_replace(",|\n"," ",$inQuery));
    
//find the words preceding and following AS
    
foreach($theWords as $theIndex => $theWord)  {
         if(
trim($theWord) == "AS"
        
&& isset($theWords[$theIndex-1])
         && isset(
$theWords[$theIndex+1])
         &&
$theWords[$theIndex+1] == $theNameOrAlias
        
) {
          
$theNameOrAlias = $theWords[$theIndex-1];
           break
1;
         }
     }
   }
   return
$theNameOrAlias;
}
?>
me at thomaskeller dot biz
24-Nov-2005 05:15
Beware that if you upgrade to MySQL 5 from any earlier version WITHOUT dumping and reloading your data (just by keeping the binary data in MyISAM table files), you might get weird output on the "table" value for mysql_fetch_field and in this function. Weird means that the table name is randomly set or not.

This behaviour seems to popup only if the SQL query contains a ORDER BY clause. A bug is already reported:

http://bugs.mysql.com/bug.php?id=14915

To prevent the issue, dump and reload all participating tables in your query or do

CREATE TABLE tmp SELECT * FROM table;
DROP TABLE table;
ALTER TABLE tmp RENAME table;

on each one via commandline client.
cptnemo
15-Aug-2004 10:18
When trying to find table names for a (My)SQL query containing 'tablename AS alias', mysql_field_table() only returns the alias as specified in the AS clause, and not the tablename.