PHP Conference Japan 2024

mysql_field_table

(PHP 4, PHP 5)

mysql_field_table取得指定欄位所在的表格名稱

警告

此擴充自 PHP 5.5.0 起已被棄用,並在 PHP 7.0.0 中移除。建議改用 MySQLiPDO_MySQL 擴充。另請參閱 MySQL:選擇 API 指南。此函式的替代方案包括:

說明

mysql_field_table(資源 $result, int $field_offset): string

傳回指定欄位所在的表格名稱。

參數

result

正在評估的結果 資源。此結果來自呼叫 mysql_query() 的結果。

field_offset

數值欄位偏移量。 field_offset0 開始。如果 field_offset 不存在,也會發出等級為 E_WARNING 的錯誤。

回傳值

成功時返回表格名稱。

範例

範例 #1 mysql_field_table() 範例

<?php

$query
= "SELECT account.*, country.* FROM account, country WHERE country.name = 'Portugal' AND account.country_id = country.id";

// 從資料庫取得結果
$result = mysql_query($query);

// 列出表格名稱,然後是欄位名稱
for ($i = 0; $i < mysql_num_fields($result); ++$i) {
$table = mysql_field_table($result, $i);
$field = mysql_field_name($result, $i);

echo
"$table: $field\n";
}

?>

注意事項

注意:

為了向後相容,可以使用以下已棄用的別名:mysql_fieldtable()

參見

新增筆記

使用者貢獻的筆記 6 則筆記

1
me at thomaskeller dot biz
19 年前
請注意,如果您從任何早期版本升級到 MySQL 5 卻沒有傾印和重新載入您的資料(僅保留 MyISAM 表格檔案中的二進位資料),則 mysql_fetch_field 和此函式的「表格」值可能會出現奇怪的輸出。奇怪是指表格名稱是隨機設定或未設定。

這種行為似乎僅在 SQL 查詢包含 ORDER BY 子句時才會出現。已回報錯誤

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

為避免此問題,請傾印並重新載入查詢中所有參與的表格,或執行以下操作

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

透過命令列客戶端在每個項目上。
0
jorge at rhst dot net
17 年前
以下函式接受一個查詢語句,並以陣列形式返回 col->table 的映射。

例如:

$query = “SELECT a.id AS a_id, b.id b_id FROM atable AS a, btable b”

$cols = queryAlias($query);

print_r($cols);

返回:

Array
(
[a] => atable
[b] => btable
)

我不能保證它是完美的,但這個函式從未在正式環境中使用,因為我最終使用了 mysqli 方法。

請享用
-Jorge

/**
* 接受一個查詢語句並返回別名->表格的映射。
*
* @param string $query
* @return array 別名映射陣列
*/

function queryAlias ( $query ) {

//全部轉換為小寫,我們忽略大小寫
$substr = strtolower($query);

//移除任何子查詢
$substr = preg_replace ( ‘/\(.*\)/’, ”, $substr);

//移除任何特殊字元
$substr = preg_replace ( ‘/[^a-zA-Z0-9_,]/’, ‘ ‘, $substr);

//移除任何空白
$substr = preg_replace(‘/\s\s+/’, ‘ ‘, $substr);

//取得 FROM 後面的所有內容
$substr = strtolower(substr($substr, strpos(strtolower($substr),‘ from ‘) + 6));

//移除任何額外的指令
$substr = preg_replace(
Array(
‘/ where .*+$/’,
‘/ group by .*+$/’,
‘/ limit .*+$/’ ,
‘/ having .*+$/’ ,
‘/ order by .*+$/’,
‘/ into .*+$/’
), ”, $substr);

//移除任何 JOIN 修飾詞
$substr = preg_replace(
Array(
‘/ left /’,
‘/ right /’,
‘/ inner /’,
‘/ cross /’,
‘/ outer /’,
‘/ natural /’,
‘/ as /’
), ‘ ‘, $substr);

//將 JOIN 語句替換為逗號
$substr = preg_replace(Array(‘/ join /’, ‘/ straight_join /’), ‘,’, $substr);


$out_array = Array();

//根據 FROM 語句分割
$st_array = split (‘,’, $substr);

foreach ($st_array as $col) {

$col = preg_replace(Array(‘/ on .*+/’), ”, $col);

$tmp_array = split(‘ ‘, trim($col));

//糟糕,發生錯誤了,讓我們繼續
if (!isset($tmp_array[0]))
continue;

$first = $tmp_array[0];

//如果設定了 “AS”,我們就包含它,如果沒有,那就表示這個表格沒有別名。
if (isset($tmp_array[1]))
$second = $tmp_array[1];
else
$second = $first;

if (strlen($first))
$out_array[$second] = $first;

}

return $out_array;
}
0
cptnemo
20 年前
當試圖查找包含 'tablename AS alias' 的 (My)SQL 查詢的表格名稱時,mysql_field_table() 只會返回 AS 子句中指定的別名,而不是表格名稱。
-1
spam at blondella dot de
18 年前
<?php
/*
這個函式或許能在上述情況中派上用場 :-)
*/
function mysql_field_table_resolve_alias($inQuery,$inResult,$inFieldName) {
$theNameOrAlias = mysql_field_table($inResult,$inFieldName);
//檢查是否使用了 AS 語法
if(ereg(" AS ",$inQuery)) {
//擷取查詢中的詞彙
$theWords = explode(" ",ereg_replace(",|\n"," ",$inQuery));
//尋找 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;
}
?>
-1
djafferian at sos dot ri dot gov
10 年前
這則注意事項可能適用於仍在 Debian 4.0 上運行 MySQL 5.0.32 的任何人。如果 SELECT 查詢使用了 GROUP BY 或 ORDER BY 並且在 FROM 子句中引用了視圖,則 mysql_field_table 函式可能會返回空的表格名稱。這是由 MySQL bug 28898 引起的,該錯誤已在 5.0.46 中修復。當我注意到在運行 MySQL 5.0.51a 的 Debian 5.0.10 伺服器上生成的生產 RSS Feed 與在運行 MySQL 5.0.32 的 Debian 4.0 測試伺服器上生成的相同 Feed 之間存在差異時,我遇到了這個問題。
-3
mehdi dot haresi at gmail dot com
15 年前
對於所有在使用表格別名的查詢中存取重複欄位名稱時遇到問題的人,我實作了以下的快速解決方案

<?php
函式 mysql_fetch_alias_array($result)
{
if (!(
$row = mysql_fetch_array($result)))
{
return
null;
}

$assoc = 陣列();
$rowCount = mysql_num_fields($result);

for (
$idx = 0; $idx < $rowCount; $idx++)
{
$table = mysql_field_table($result, $idx);
$field = mysql_field_name($result, $idx);
$assoc["$table.$field"] = $row[$idx];
}

return
$assoc;
}
?>

假設我們有兩個資料表,student 和 contact,各自都有 fID 作為索引欄位,並且希望在 PHP 中存取這兩個 fID 欄位。

這個函式的用法與呼叫 mysql_fetch_array 非常相似。

<?php
$result
= mysql_query("select * from student s inner join contact c on c.fID = s.frContactID");

while (
$row = mysql_fetch_alias_array($result))
{
echo
"學生 ID: {$row['s.fID']}, 聯絡人 ID: {$row['c.fID']}";
}
?>

就這樣:)

請注意,使用此函式時,您必須使用別名(例如 s.Name、s.Birthday)來存取所有欄位,即使它們沒有重複。

如果您有任何問題,請寄信給我。

此致,
Mehdi Haresi
die-webdesigner.at
To Top