PHP 下 SQLite3 的没有 num_rows 的问题

该文章根据 CC-BY-4.0 协议发表,转载请遵循该协议。
本文地址:https://fenying.net/post/2014/12/21/no-num-rows-for-sqlite3-in-php/

最近在项目里用到了SQLite,而 PHP 中的 SQLite3 类去没有提供类似 MySQL 里的 num_rows 函数。官方说法是 SQLite3 并不缓冲结果集,所以无法直接获取 num_rows

原文如下:

[2009-08-20 09:40 UTC] [email protected]

SQLite and SQLite3 have different APIs.

SQLite3 doesn’t implement buffered results so the total number of results isn’t available. This is down to the fact that libsqlite really doesn’t implement it either and its emulated by the old SQLite extension.

所以获取的方法只能有3个:

  1. 改用 PDO,即 $db = new PDO('sqlite:xxxx.sql3');2015-07-13更新:经测试,此法无效
  2. 在查询完成后立即循环到底,并计数;
  3. 使用 COUNT(*) 再查询一次。
comments powered by Disqus