MIPS64 Posted October 19, 2012 Share Posted October 19, 2012 (edited) I am struggling to return a row count for a query I have to run on a database using OBDC connection to a MSSQL server. I can get row counts just fine with MySQL. I know the query works because I can run it on the MSSQL server with no problem. When I echo the result I get -1. I am not sure what that means. $sth = $dbh->prepare(" SELECT PatronID FROM Patrons WHERE Barcode = :barcode "); $sth->bindParam(':barcode', $barcode); $sth->execute(); $count = $sth->rowCount(); echo $count; EDIT: I can echo the PatronID just fine. Just not retrieve the row count. Edited October 19, 2012 by MIPS64 Quote Link to comment https://forums.phpfreaks.com/topic/269680-pdo-and-rowcount-with-obdc-driver/ Share on other sites More sharing options...
scootstah Posted October 19, 2012 Share Posted October 19, 2012 (edited) From the manual: For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action. So, you can either run two queries or you can count() the result set. Edited October 19, 2012 by scootstah Quote Link to comment https://forums.phpfreaks.com/topic/269680-pdo-and-rowcount-with-obdc-driver/#findComment-1386367 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.