开发者

return total of stmt->execute()

开发者 https://www.devze.com 2023-03-30 14:41 出处:网络
How do I find out the number of rows return from a query when using stmt->prepare(); / stmt->execute();

How do I find out the number of rows return from a query when using stmt->prepare(); / stmt->execute();

In my example below, if i don't get any results ba开发者_开发问答ck, i would like to display an nice message to the user

Any ideas how to achieve this?

Thanks

EDITED: as stated by @zerkms, there is a solution but, i was hoping for a solution that doesn't buffer the entire result set in the statement handle...

....

$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
}
...


There is a method for that: mysqli_stmt_num_rows() / mysqli_stmt::num_rows()


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

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

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

精彩评论

暂无评论...
验证码 换一张
取 消