PDO::rowCount() returning -1

前端 未结 2 1730
不思量自难忘°
不思量自难忘° 2021-01-15 03:25

i have this function below. that i used to run on a MySQL DB. I had to move to a SQL Server 2008, easy thing.

But, after that, the rowCount()

相关标签:
2条回答
  • 2021-01-15 03:38

    Got it guys.

    The prepare() statement should receive one more parameter. array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL) .

    $stmt = $this->pdo->prepare($sql,array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));

    After that the rowCount() should work fine.

    Thanks for the answer @Rasclatt.

    0 讨论(0)
  • 2021-01-15 04:02

    I think you may be running into the gotcha on the PDO rowCount() function. As stated in the PHP manual. The second sentence is the gotcha...:

    "If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behavior is not guaranteed for all databases and should not be relied on for portable applications."

    Instead, you should use a COUNT(*) sql statement.

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