取得結果中指定欄位的欄位名

mysql_field_name

(PHP 4, PHP 5)

mysql_field_name 取得結果中指定欄位的欄位名

說明

mysql_field_name(resource $result, int $field_index): string

mysql_field_name() 返回指定欄位索引的欄位名。result 必須是一個合法的結果識別符號,field_index 是該欄位的數字偏移量。

注意:

field_index 從 0 開始。

例如,第三個欄位的索引值其實是 2,第四個欄位的索引值是 3,以此類推。

注意: 此函式返回的欄位名大小寫敏感

示例 #1 mysql_field_name() 例子

<?php
/* The users table consists of three fields:
 *   user_id
 *   username
 *   password.
 */
$link mysql_connect('localhost'"mysql_user""mysql_password");
$dbname "mydb";
mysql_select_db($dbname$link)
    or die(
"Could not set $dbname: " mysql_error());
$res mysql_query("select * from users"$link);

echo 
mysql_field_name($res0) . "\n";
echo 
mysql_field_name($res2);
?>

以上例子將產生如下輸出:

user_id
password

為向下相容仍然可以使用 mysql_fieldname(),但反對這樣做。

參數

result

resource 型的結果集。此結果集來自對 mysql_query() 的呼叫。

field_offset

數值型欄位偏移量。 field_offset0 開始。如果 field_offset 不存在,則會發出一個 E_WARNING 級別的錯誤

返回值

The name of the specified field index on success 或者在失敗時返回 false.

範例

示例 #2 mysql_field_name() example

<?php
/* The users table consists of three fields:
 *   user_id
 *   username
 *   password.
 */
$link mysql_connect('localhost''mysql_user''mysql_password');
if (!
$link) {
    die(
'Could not connect to MySQL server: ' mysql_error());
}
$dbname 'mydb';
$db_selected mysql_select_db($dbname$link);
if (!
$db_selected) {
    die(
"Could not set $dbname: " mysql_error());
}
$res mysql_query('select * from users'$link);

echo 
mysql_field_name($res0) . "\n";
echo 
mysql_field_name($res2);
?>

以上例程會輸出:

user_id
password

註釋

注意: 此函式返回的欄位名大小寫敏感

注意:

爲了向下相容,可以使用下列已廢棄的別名: mysql_fieldname()

參見

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *