mysql_num_rows() in pdo?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Wilbo
Forum Newbie
Posts: 22
Joined: Fri Jul 25, 2008 5:45 am

mysql_num_rows() in pdo?

Post by Wilbo »

Hi,

I was wondering if anyone can tell me the best practice for replacing mysql_num_rows() in pdo?
I just want to test if something exisits in a db, like an email for example.

I would normally have used something like:

Code: Select all

//make sure the email address is available
$query = "SELECT user_id FROM users WHERE email='$e'";
$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
		
if (mysql_num_rows($result) == 0) { //available
     //do whatever
}
what is the excepted way to achieve this in pdo?

Thanks in advance!
Wilbo
Forum Newbie
Posts: 22
Joined: Fri Jul 25, 2008 5:45 am

Re: mysql_num_rows() in pdo?

Post by Wilbo »

I found this at http://net.tutsplus.com/tutorials/php/w ... se-access/

Code: Select all

$sql = "SELECT COUNT(*) FROM folks";  
if ($STH = $DBH->query($sql)) {  
    # check the row count  
    if ($STH->fetchColumn() > 0) {  
  
    # issue a real select here, because there's data!  
    }  
    else {  
        echo "No rows matched the query.";  
    }  
}


which is what I'm looking for I suppose. The issue I have is what is the best way to use the try/ catch statements. I have the feeling that I could be writing much more efficient code by better usage. Anyone have any thoughts?
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: mysql_num_rows() in pdo?

Post by mikosiko »

Post Reply