PHP

PHP中mysqli_num_rows怎么使用

小亿
95
2024-02-05 17:25:44
栏目: 云计算

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查询并获取了结果集。

0
看了该问题的人还看了