douzha8489 2015-07-22 17:08
浏览 99

PDO相当于mysql_num_rows或mssql_num_rows

I know this question has been asked before but it seems like the solutions have been specific to the problem presented.

I have a codebase with hundreds of instances where mssql_num_rows is used.

Code example:

$db->execute($sql);

if ($db->getRowsAffected() > 0) {
    $total = $db->fetch();

In db class:

$this->rowsaffected = mssql_num_rows($this->query_result);
  • I can't create generic SELECT count(*) FROM table queries as I have too many specific select statements.
  • I could run a preg_replace to remove everything between SELECT and FROM and then replace with a COUNT(*) and run a second query but this assumes all queries are setup a certain way.
  • I could fetchAll first then count() the results but that means upgrading all instances of the if statements.

So what is the best all around replacement to a *_num_rows function if people are updating their code to PDO. Not something that solves a specific problem, but something that replaces the functionality of *_num_rows. If that's not possible what allowed it to be possible before?

  • 写回答

2条回答 默认 最新

  • donxbje866688 2015-07-22 17:11
    关注

    If you want to count the rows you can do this with PDO:

    $sql = 'select * from users';
    $data = $conn->query($sql);
    $rows = $data->fetchAll();
    $num_rows = count($rows);
    

    There is no way to directly count rows when using a SELECT statement with PDO.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?