Boolean Island !

Get the Row Number in PDO

Posted by: ranacse05 on: July 8, 2008

I was working on PHP Data Object (PDO) . But i saw that PDOstatement::rowCount()  does not return row number for mysql when u use the SELECT command . Then i found this function to get the row number after a SELECT query .

<?
function getRowCount($sql)
{
$sql = trim($sql);
$sql = preg_replace(‘~^SELECT\s.*\sFROM~s’, ‘SELECT COUNT(*) FROM’,
$sql);
$sql = preg_replace(‘~ORDER\s+BY.*?$~sD’, ”, $sql);
$stmt = $dbh->query($sql);
$r = $stmt->fetchColumn(0);
$stmt->closeCursor();
return $r;
}
?>

Now call it like this

$sql = “SELECT name,email FROM user where userid=’$id’ “;
$totalBooks = getRowCount($sql);

This will return the effected row after the SELECT command .

5 Responses to "Get the Row Number in PDO"

SELECT doesnt affect anything. only UPDATE and DELETE, INSERT DOES. so rowCount() wont return anything for select.

to get the number of rows returned, why dont u use just count() function treating it as array?

@Hasin Bhai
thanks for ur advice . Its so easy . :D

Umm… I don’t get it. How come rowCount works for me but not for you? :o

Two tables; foo and faa. Foo table contains 4 rows, and faa 0. If I do query:
$fa = $db->query(“SELECT * FROM foo”);
$fo = $db->query(“SELECT * FROM faa”);

print $fa->rowCount().”".$fo->rowCount();

I get results:
4
0

Which should be correct. Am I missing something? Yes, I am using PDO

I wasn’t working with me . And as the php.net says that its only work when the query change the database .

It works for some DBs.

Leave a Reply

My blog worth


My site is worth $3431.
How much is yours worth?

My Twittes

FireFox

Firefox 3

My Facebook

this is Raquibul's profile

View Raquibul Islam's profile on LinkedIn

My fav

 

July 2008
S M T W T F S
« Jun   Aug »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Flickr Photos

Moon !

Moon !

Leevio

Leevio

Leevio

Tea !

History

me

More Photos

My Bookmarks

Dynamic Malloc

Blog Stats

  • 45,997 hits