php - Pdo count rows from SELECT query with POST params -


from manual:

pdostatement::rowcount() returns number of rows affected last delete, insert, or update statement executed corresponding pdostatement object.

if last sql statement executed associated pdostatement select statement, databases may return number of rows returned statement. however, behaviour not guaranteed databases , should not relied on portable applications.

so pdostatement::rowcount() isn't working me select queries. have query must able add and conditions where clause this:

$sql = "select * customers 1=1";  if ($name)     $sql .= " , `name` :name"; if ($type)     $sql .= " , `type` = :type"; if ($category)     $sql .= " , `category` = :category"; if ($group)     $sql .= " , `group` = :group"; if ($area)     $sql .= " , `area` = :area";  $query = $pdo->prepare($sql);  if ($name)     $query->bindvalue(':name', '%' . $name . '%'); if ($type)     $query->bindvalue(':type', $type); if ($category)     $query->bindvalue(':category', $category); if ($group)     $query->bindvalue(':group', $group); if ($area)     $query->bindvalue(':area', $area);  $query->execute(); 

do need following code or there easier way it?

$sql = "select * customers 1=1"; $count = "select count(*) customers 1=1";  if ($name) {     $sql .= " , `name` :name";     $count .= " , `name` :name"; } if ($type) {     $sql .= " , `type` = :type";     $count .= " , `type` = :type"; } if ($category) {     $sql .= " , `category` = :category";     $count .= " , `category` = :category"; } if ($group) {     $sql .= " , `group` = :group";     $count .= " , `group` = :group"; } if ($area) {     $sql .= " , `area` = :area";     $count .= " , `area` = :area"; }  $query = $pdo->prepare($sql);  if ($name)     $query->bindvalue(':name', '%' . $name . '%'); if ($type)     $query->bindvalue(':type', $type); if ($category)     $query->bindvalue(':category', $category); if ($group)     $query->bindvalue(':group', $group); if ($area)     $query->bindvalue(':area', $area);  $query->execute(); 

although rowcount() working, don't need anyway. that's useless function selects.

you either need data or count, not both. since you're selecting data, don't need rowcount() then. count data.

in case need count no data, ought select count right database. means don't need rowcount() again.


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -