php mysql绑定结果查询时stmt获得查询结果条数

在php中查询mysql数据库时,如果使用绑定变量的方法可以提高效率,关于插入数据的用法,php和mysql Web开发宝典有比较完整的实例,因此我尝试使用绑定变量的方法进行查询操作,结果可以查询出来,但是没有办法在一开始就获得查询记录总条数,最后终于在php.net找到了答案:

http://php.net/manual/en/mysqli-stmt.num-rows.php

用法如下:

<?php
/* Open a connection */
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20";
if ($stmt = $mysqli->prepare($query)) {

    /* execute query */
    $stmt->execute();

    /* store result */
    $stmt->store_result();

    printf("Number of rows: %d.\n", $stmt->num_rows);

    /* close statement */
    $stmt->close();
}

/* close connection */
$mysqli->close();
?>

面向过程的请自己查看。

你可能感兴趣的:(PHP,mysql,store_result,stmt,num_rows)