Hi All.
Have been messing around with PDO for a while, but have come across a bit of a quandry. I like the prepare - execute method for fetching data, but I needed to check if an update query actually did update or not. Now not having mysql_affected_rows() to hand, I was left scratching my head. PDO:exec can do this, but it seems only on 'straight unprepared queries'. Does anybody here have any suggestions on the best way to implement a PDO::exec but on a prepared query?

This is some cut-down of my code, so you get the idea:

public function setEmail($newemail){

    $statement = $this->db->prepare("UPDATE users SET email = :email WHERE user_id = :id");
    $statement->execute(array(':email' => $newemail, ':id' => $this->id));


Now, I'm looking to do something like this:

$count = $this->db->exec("UPDATE users SET email = '$newemail' WHERE user_id = $this->id");
return $count;

But parameterized/prepared. Any thoughts gratefully received.

4 Years
Discussion Span
Last Post by diafol

Doh! Must have been having a funny turn. I had the code all along, but I couldn't get it to work. For anybody wondering - for the prepare-execute method:

public function changeEmail($newemail){
    $statement = $this->db->prepare("UPDATE users SET email = :email WHERE user_id = :id");
    $statement->execute(array(':email' => $newemail, ':id' => $this->id));
    return $statement->rowCount();

I was using the same $newemail value that was already in the DB - so there was nothing to update. That's why I was returning 0 every time.

For the record, I can't see how PDO::exec() can work with params due to the way it works like mysql_query(), but if anybody knows of such a method, please post back.

Edited by diafol


I can't see how PDO::exec() can work with params

Because it can't ;) prepare/execute/rowCount is the way to go.


Yep, had a feeling that was the case. Just needed it confirmed. Thanks p.


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.