Function name: mysql_field_len()
Applicable version: PHP 4, PHP 5, PHP 7
Usage: The mysql_field_len() function is used to get the length (number of characters) of the specified field.
Syntax: int mysql_field_len ( resource $result , int $field_offset )
parameter:
Return value: When successful, return the field length (in units of characters); when failed, return FALSE.
Example:
Suppose there is a table named "users" that contains the following fields: id, name, email.
<?php // 连接到数据库$link = mysql_connect('localhost', 'user', 'password'); if (!$link) { die('连接数据库失败: ' . mysql_error()); } // 选择数据库$db_selected = mysql_select_db('database_name', $link); if (!$db_selected) { die ('选择数据库失败: ' . mysql_error()); } // 执行查询语句$result = mysql_query("SELECT * FROM users"); // 获取字段长度$field_len = mysql_field_len($result, 1); // 获取"name" 字段的长度echo "name 字段长度为:" . $field_len; // 释放结果资源mysql_free_result($result); // 关闭数据库连接mysql_close($link); ?>
In the above example, we first connect to the database, select the database, and then execute the query statement. Next, use the mysql_field_len() function to get the length of the "name" field and print the result to the screen. Finally, free the result resource and close the database connection.
Note that because the mysql_field_len() function has been deprecated in PHP 7.0.0, it is recommended to use the mysqli or PDO extension instead of the MySQL extension.