mysqli_field_count()函數(shù)返回最近查詢的列數(shù)。
mysqli_field_count()函數(shù)用于獲取最近執(zhí)行的MySQL查詢的結(jié)果集中的字段(列)數(shù)。
mysqli_field_count($con)
序號 | 參數(shù)及說明 |
---|---|
1 | con(必需) 這是一個(gè)表示與MySQL Server的連接的對象。 |
mysqli_field_count()函數(shù)返回一個(gè)整數(shù)值,該值指示最后一個(gè)查詢的結(jié)果集中的列數(shù)。如果最后一個(gè)查詢不是SELECT查詢(無結(jié)果集),則此函數(shù)返回0。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_field_count()函數(shù)的用法(面向過程風(fēng)格)-
<?php //建立連接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //查詢以檢索employee表的所有記錄 mysqli_query($con, "Select * from employee"); //字段數(shù) $count = mysqli_field_count($con); print("字段數(shù): ".$count); //關(guān)閉連接 mysqli_close($con); ?>
輸出結(jié)果
字段數(shù): 6
在面向?qū)ο蟮臉邮街?,此函?shù)的語法為$con->field_count;。,其中$con是連接對象:
<?php //建立連接 $con = new mysqli("localhost", "root", "password", "mydb"); //查詢以檢索employee表的所有記錄 $con -> query("Select FIRST_NAME, LAST_NAME, AGE from employee"); //字段數(shù) $count = $con->field_count; print("字段數(shù): ".$count); //關(guān)閉連接 $con -> close(); ?>
輸出結(jié)果
字段數(shù): 3
以下是mysqli_field_count()函數(shù)的另一個(gè)示例
<?php //建立連接 $con = mysqli_connect("localhost", "root", "password", "mydb"); print("字段數(shù): ".mysqli_field_count($con)."\n"); //插入查詢 mysqli_query($con, "INSERT INTO employee (FIRST_NAME, AGE) VALUES (Archana, 25), (Bhuvan, 29)"); print("字段數(shù): ".mysqli_field_count($con)); //關(guān)閉連接 mysqli_close($con); ?>
輸出結(jié)果
字段數(shù): 0 字段數(shù): 0
返回最近查詢的列數(shù):
<?php $connection_mysql = mysqli_connect("localhost","root", "password", "mydb"); if (mysqli_connect_errno($connection_mysql)){ echo "連接MySQL失敗: " . mysqli_connect_error(); } mysqli_query($connection_mysql,"SELECT * FROM employee"); print(mysqli_field_count($connection_mysql)); mysqli_close($connection_mysql); ?>
輸出結(jié)果
6