6

I have a pretty simple UPDATE query which works when I execute it in PHPMyAdmin (so it's not an SQL syntax error). However, when I try to execute the query with PHP PDO, no error is displayed and not a single row is affected by the query.

I have error reporting set like this: PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION. But no error is shown. My PHP code looks like this:

try {
    $sql = "UPDATE user 
            SET username = :username, 
                activation_token = :activation_token, 
                activation_date_time = :activation_date_time, 
                activation_status  = :activation_status  
            WHERE activation_token = :current_activation_token";
    $stmt = $dbh->get_instance()->prepare($sql); 

    $stmt->bindParam(':username', $username, PDO::PARAM_STR);       
    $stmt->bindParam(':activation_token', $empty_activation_token, PDO::PARAM_STR);    
    $stmt->bindParam(':activation_date_time', $datetime, PDO::PARAM_STR);
    $stmt->bindParam(':activation_status', $active_status, PDO::PARAM_STR); 
    $stmt->bindParam(':current_activation_token', $activation_token, PDO::PARAM_STR);
    $stmt->execute();

    echo 'username: ' . $username;
} 
catch(PDOException $e) {
    echo $e;
} 

I've also checked if I was somehow using reserved keywords or anything like that, but I'm pretty sure that's not the case. Why is this not working?

erol_smsr
  • 1,454
  • 4
  • 24
  • 49
  • 3
    I think you need $e->getMessage() to actually see any errors? – BAGman Jun 05 '15 at 12:11
  • Yes you are right. sorry i didn't check that one.Please do that `@erol_smsr` – Alive to die - Anant Jun 05 '15 at 12:12
  • 3
    *"I have error reporting set like this: PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION."* - That should be `$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` right after the connection is opened, if you're not already doing it that way. Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Jun 05 '15 at 12:13
  • @BAGman I see errors when I make other mistakes, so I don't think your solution will make a difference. @Saty `$empty_activation_token = 'empty';` – erol_smsr Jun 05 '15 at 12:15
  • 2
    @erol_smsr nonetheless, `echo $e;` does nothing. You need to do `catch(PDOException $e) { print $e->getMessage(); }` along with my other comment about error reporting etc. – Funk Forty Niner Jun 05 '15 at 12:19
  • try to check that all variables values are coming perfectly or not? – Alive to die - Anant Jun 05 '15 at 12:19
  • and what the problem to do `$e->getMessage();`, do it and check once. – Alive to die - Anant Jun 05 '15 at 12:20
  • @Fred-ii- You can set PDO error reporting as `PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION` in the driver options array argument of the PDO contructor – Nev Stokes Jun 05 '15 at 12:55
  • Double check if there are at least one matching row to update with `$activation_token` as identifier. It seems the statement can't find something to update. – Dietmar Jun 05 '15 at 12:59
  • I've added `error_reporting(E_ALL); ini_set('display_errors', 1);` which now gives me an error about a variable which is not defined. It's the variable that's supposed to be the value of the WHERE condition. That's why the query isn't being executed properly I guess. I'm going to try to fix the undefined variable and see if that fixes this problem. – erol_smsr Jun 05 '15 at 16:00

0 Answers0