要在 PHP 中獲取查詢結(jié)果的 num_rows(即查詢返回的行數(shù)),您需要使用 MySQLi 或 PDO 擴(kuò)展
<?php
// 連接到數(shù)據(jù)庫(kù)
$mysqli = new mysqli("localhost", "username", "password", "database");
// 檢查連接
if ($mysqli->connect_error) {
die("連接失敗: " . $mysqli->connect_error);
}
// 查詢
$sql = "SELECT * FROM table_name";
$result = $mysqli->query($sql);
// 獲取 num_rows
$num_rows = $result->num_rows;
echo "查詢結(jié)果的行數(shù): " . $num_rows;
?>
<?php
// 連接到數(shù)據(jù)庫(kù)
$dsn = "mysql:host=localhost;dbname=database;charset=utf8";
$pdo = new PDO($dsn, "username", "password");
// 查詢
$sql = "SELECT * FROM table_name";
$stmt = $pdo->prepare($sql);
$stmt->execute();
// 獲取 num_rows
$num_rows = $stmt->rowCount();
echo "查詢結(jié)果的行數(shù): " . $num_rows;
?>
請(qǐng)根據(jù)您的實(shí)際情況修改數(shù)據(jù)庫(kù)連接參數(shù)(主機(jī)名、用戶名、密碼和數(shù)據(jù)庫(kù)名)以及表名。這兩個(gè)示例將顯示查詢結(jié)果的行數(shù)。