Member Avatar

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.

Recommended Answers

All 3 Replies

Member Avatar

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.

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

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

Member Avatar

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


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.