genista wrote:I am working on an infinite scroll script and am having trouble getting the number of rows using PDO.
Do you really need the number of rows? If not, it would be better to order by say, date/time, then retrieve based on the newest/oldest row in the previous result set. If that column is indexed, this tends to be more efficient than using LIMIT. Typically, the LIMIT method is convenient when you want pagination, but in an "infinite scroll", you don't: you just need to keep retrieving the next set of results.
genista wrote:In the following snippet on the last line I am getting an error: Call to undefined method PDOStatement::query(), what should replace 'query' here?
Refer to the PHP manual concerning the PDO extension. You will see that the query method is a member of the PDO class, but once you have called prepare on an object of the PDO class to get a PDOStatement object, you would use the execute method of the PDOStatement class.
What you probably wanted to do is this:
$sql = "SELECT COUNT(*) AS row_count FROM users WHERE userid=:userid";
$statement = $DB_con->prepare($sql);
$statement->bindParam(':userid', $userid, PDO::PARAM_INT);
$statement->execute();
$result = $statement->fetch();
$rows_returned = $result['row_count'];
Remember to do some error checking though, but of course if you use my suggestion of retrieving based on the previous result set, this counting of rows would be unnecessary.