Can't return correct row count from database using php pdo

前端 未结 2 1234
庸人自扰
庸人自扰 2021-01-28 19:16

I am trying to return the row count from a table in my database but continue to get the wrong value. I need the row count to process subset values for pagination. There are 11 i

2条回答
  •  没有蜡笔的小新
    2021-01-28 19:52

    SELECT COUNT(*) FROM tableName just returns a single row. The data in that row is the number of rows in the table. So $query->rowCount() will always be 1. If you want the number of rows, you need to use:

    $query = $pdo->prepare("SELECT COUNT(*) ct FROM articles");
    $query->execute();
    $rowCount = $query->fetch(PDO::FETCH_OBJ)->ct;
    

提交回复
热议问题