导航首页 » 技术教程 » PHP:MySQL函数mysql_field_name()的用法
PHP:MySQL函数mysql_field_name()的用法 155 2023-12-14   

mysql_field_name

(PHP 4, PHP 5)

mysql_field_name — 取得结果中指定字段的字段名

说明

string mysql_field_name ( resource $result , int $field_index )

mysql_field_name() 返回指定字段索引的字段名。result 必须是一个合法的结果标识符,field_index 是该字段的数字偏移量。

Note:

field_index 从 0 开始。

例如,第三个字段的索引值其实是 2,第四个字段的索引值是 3,以此类推。

Note: 此函数返回的字段名大小写敏感。

Example #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($res, 0) . "n";
echo mysql_field_name($res, 2);
?>

以上例子将产生如下输出:

user_id
password

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

参数

result

resource 型的结果集。此结果集来自对 mysql_query() 的调用。

field_offset

数值型字段偏移量。 field_offset 从 0 开始。如果 field_offset 不存在,则会发出一个 E_WARNING 级别的错误

返回值

The name of the specified field index on success 或者在失败时返回 FALSE.

范例

Example #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($res, 0) . "n";
echo mysql_field_name($res, 2);
?>

以上例程会输出:

user_id
password

注释

Note: 此函数返回的字段名大小写敏感。

Note:

为了向下兼容,可以使用下列已废弃的别名: mysql_fieldname()

参见

mysql_field_type() - 取得结果集中指定字段的类型 mysql_field_len() - 返回指定字段的长度


!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103