Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > PHP: Базы Данных > PDO - количество возвращаемых записей


Автор: IZ@TOP 22.9.2006, 13:20
В либе PHP для работы с mysql имеется функция mysql_num_rows, с помощью нее можно узнать количество записей которые мы можем получить после запроса типа SELECT.

В PDOStatement же, я ни как не могу найти для этого метода. PDOStatement::rowCount() не предлагать, ибо:
Цитата

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.


Облазил пол инета, нашел упоминание о некоей не буферизованной выборке - меня это несколько смутило. Браться за эту нить не стал. Зато видел кучу вариантов где предлагается сделать:
Код

...
$count = count($stmt->fetchAll());
...

Что естественно меня не совсем устраивает.

Автор: Wowa 22.9.2006, 13:33
Цитата

Q: Why doesn't PDO provide a way to retrieve the number of rows in a result set? Does PDO support buffered result sets? Why does PDO provide a way to seek to a specific row?
A: PDO provides only unbuffered result sets. This means that when using PDO the client will immediatly allow you to process the returned rows even if the entire result sets has not been assembled by the database. Also the client will only receive the rows that are actually fetched. This will improve the memory requirements considerably with large result sets. If however you require the convinience of being able to jump around in the result set or you need to get the number of rows in the result set you can use the fetchAll() method to fetch the entire result set. This will fetch the entire result set into an array. Also often when people think they need to check the number of rows in a result set they actually just want to know if there are any rows in the result set. By restucturing the relevant code it should be easily possible to simply issue a first fetch and get the same information in a more efficient manner. 

Автор: IZ@TOP 22.9.2006, 13:40
Wowa, я в некоторой степени согласен с утверждениями из приведенной тобой цитаты, хотя английский мой не очень хорош, но более или менее я вник в тему.
Знаешь ли, очень тяжело иногда отказываться от привычных действий, но по всей видимости, выбора у меня особого нет. Да, и, если это действительно скажется на производительсности, я не против использования такого подхода.

Автор: Рыжий 21.2.2007, 18:28
Можно сделать так:

Код

      $result = $db->query("select count(*) from `links`");
      $count=$result->fetch(PDO::FETCH_NUM);    
      echo $count[0];
      $result=null;

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)