Work-around for PHP5's PDO rowCount MySQL issue

前端 未结 3 1376
小蘑菇
小蘑菇 2020-12-02 21:15

I\'ve recently started work on a new project using PHP5 and want to use their PDO classes for it. The problem is that the MySQL PDO Driver doesn\'t support rowCount() so the

相关标签:
3条回答
  • 2020-12-02 21:55

    For those of you who are using MySQL stored procedures, this solution isn't really feasible. What I would suggest that you do is have your stored procedure create two rowsets. The first one will contain one row and one column, containing the number of records. The second will be the recordset you will use for fetching that number of rows.

    The number of unlimited rows can be a SELECT COUNT(*) with the exact same WHERE clause as the second rowset without the LIMIT/OFFSET clauses.

    Another idea could be to create a temporary table. Use your SELECT statement to populate the temporary table. Then you can use SELECT COUNT(*) FROM tmpTable for your first rowset and SELECT * FROM tmpTable for your second.

    0 讨论(0)
  • 2020-12-02 22:09

    You can issue a SELECT FOUND_ROWS() query right after the original SELECT query to get row count.

    $pdo->query("SELECT * FROM users");
    $foundRows = $pdo->query("SELECT FOUND_ROWS()")->fetchColumn();
    

    See also: MySQL Docs on FOUND_ROWS()

    0 讨论(0)
  • 2020-12-02 22:15

    This question is based on several false assumptions and one outdated statement.

    First of all, do not confuse number of affected and selected rows. PDO supported the former even back in '09.

    Speaking of number of rows returned by SELECT statement - you just don't need that number. The data you have is enough.

    And yeah, nowadays rowCount() supports number of rows selected from mysql as well. But again - you don't need that number in an average web-application anyway.

    0 讨论(0)
提交回复
热议问题