doutuo3899 2015-06-11 22:37
浏览 139

如何在结果集中获取行计数和循环

I've recently gotten over my bad habit of using the deprecated mysql functions in favor of mysqli, and I'm having some issues.

Right now, I'm using something similar to the following:

$query = $conn->prepare("SELECT * FROM table WHERE cid = ?");
$query->bind_param('i', $id);
$query->execute();

And this is where I get stuck. In order to loop through the result, I use:

while ($row = $query->get_result()->fetch_assoc()) {
    //My code here
}

However, I need to determine the number of rows returned from the query before going through the results. To do this, I need to do the following:

$query->store_result();
$rows = $query->num_rows;

But I get errors when calling both get_result and store_result on the same query.. is there an easier way to do this? Am I overthinking things? I basically just want to determine if the result set has greater than x # of rows, and if so, loop through the results.

Thanks for any help.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度