mysqli_num_rows函數用于獲取結果集中的行數。使用方法如下:
1.創建數據庫連接:
$conn = mysqli_connect("localhost", "username", "password", "database_name");
if (!$conn) {
die("連接失敗: " . mysqli_connect_error());
}
2.執行SQL查詢并獲取結果集:
$sql = "SELECT * FROM table_name";
$result = mysqli_query($conn, $sql);
3.使用mysqli_num_rows函數獲取結果集中的行數:
$row_count = mysqli_num_rows($result);
echo "行數:" . $row_count;
完整示例代碼如下:
$conn = mysqli_connect("localhost", "username", "password", "database_name");
if (!$conn) {
die("連接失敗: " . mysqli_connect_error());
}
$sql = "SELECT * FROM table_name";
$result = mysqli_query($conn, $sql);
$row_count = mysqli_num_rows($result);
echo "行數:" . $row_count;
mysqli_close($conn);
注意:在使用mysqli_num_rows函數之前,需要確保已經執行了SQL查詢并獲取了結果集。