作者:jkjkjd_105 | 来源:互联网 | 2023-06-29 23:38
如题,请教下 php使用mysqli连接,如何获取表的字段名呢? 如使用mysql_field_name
麻烦知道的说下,谢谢!
3 个解决方案
mysqli_result::fetch_fields
mysqli_fetch_fields
(PHP 5)
mysqli_result::fetch_fields -- mysqli_fetch_fields — Returns an array of objects representing the fields in a result set
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * from yourtablename";
if ($result = mysqli_query($link, $query)) {
/* Get field information for all columns */
$finfo = mysqli_fetch_fields($result);
foreach ($finfo as $val) {
printf("Name: %s\n", $val->name);
printf("Table: %s\n", $val->table);
printf("max. Len: %d\n", $val->max_length);
printf("Flags: %d\n", $val->flags);
printf("Type: %d\n\n", $val->type);
}
mysqli_free_result($result);
}
/* close connection */
mysqli_close($link);
?>