返回总计stmt-> execute()

时间:2011-08-23 05:17:42

标签: php mysqli

当使用stmt-> prepare()时,如何找出查询返回的行数? / stmt-> execute();
在下面的例子中,如果我没有得到任何结果,我想向用户显示一条好消息

任何想法如何实现这一目标?

由于

编辑: 正如@zerkms所述,有一个解决方案但是,我希望有一个解决方案不会在语句句柄中缓冲整个结果集......

...

$selected_disc = array(1=>'disc1', 2=>'disc2', 3=>'disc3');

// show tracks for each disc in album
$sql = 'SELECT track_id, name FROM album WHERE album_id = ?';

foreach ($selected_disc as $key => $disc) {

    $stmt->prepare($sql);
    $stmt->bind_param('i', $key);
    $ok = $stmt->execute();
    $stmt->bind_result($trackid, $name);

    if( VALUES FOUND, SHOW !) {

        while ($stmt->fetch()) {
            echo $t_name;
        }

    } else {
        echo 'Nothing to display... blablabla';
    }

    $stmt->free_result(); // free the database resources for other queries
}
...

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

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

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

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