Jump to content

Recommended Posts

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 by MIPS64
Link to comment
https://forums.phpfreaks.com/topic/269680-pdo-and-rowcount-with-obdc-driver/
Share on other sites

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 by scootstah
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.