So i have a simple script nothing to big, but im already getting an unexpected "updateTime"

 $sql = "UPDATE users SET last_login = NOW() ";
 $updateTime = $this->db_connection->exec($sql);

THis is my first time using PDO and I am already getting errors after 2 lines...

Okay, so I got the first bug to work, but now it will not update the date time...

                    $nid = $_SESSION['user_id'];
                    $date = date("Y-m-d H:i:s");
                    $stmt = $this->db_connection->prepare("UPDATE `users` SET `last_login` = ? WHERE `user_id` = ? ");
                    $stmt->execute(date("Y-m-d H:i:s"), '$nid');

                    echo "Date: ".var_dump($date);
                    echo "User ID: ".var_dump($nid);

Here is the result set that is returned...

  object(mysqli_stmt)#5 (10) { ["affected_rows"]=> int(0) ["insert_id"]=> int(0) ["num_rows"]=> int(0)     ["param_count"]=> int(2) ["field_count"]=> int(0) ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["sqlstate"]=> string(5) "00000" ["id"]=> int(1) } Statement: string(19) "2017-06-05 22:13:56" Date: string(1) "1" User ID: 

Okay, so last one...

fixed that issue:

  $stmt = $this->db_connection->prepare("UPDATE `users` SET last_login = ? WHERE user_id = ? ");
                    $stmt->execute(array(date("Y-m-d H:i:s"), $nid));
                    print_r( $stmt );

Still will not update though...

here is what it returns...

mysqli_stmt Object
[affected_rows] => 0
 [insert_id] => 0
 [num_rows] => 0
 [param_count] => 2
 [field_count] => 0
 [errno] => 0
 [error] => 
 [error_list] => Array

[sqlstate] => 00000
[id] => 1


Member Avatar


Does the $nid exists in the DB. There is no error, so I'm assuming there is nothing to update as the user doesn't exist.

Hey Diafol, I am using this to set $nid : $nid = $_SESSION['user_id'];. The user is me, so when it shows 1, that is me in the DB for user_id field.

I would double check if $_SESSION['user_id'] has anything in there...

If the user does not exist then the affected_rows will be 0
It could be that the data you want to update already matches the data that you are passing.. so no need to update..
therefore the affected_rows will be 0

Member Avatar


Echo the nid to see. I'm assuming you have session_start() to ensure session vars carried on.

Member Avatar


Actually from the stmt obj you do seem to be passing the right parameters. My mistake.

Hey Diafol, I echoed the $nid variable and it does show my user_id. Since i am the first one in the database, it echos 1. I do have the session_start() variable listed. I cannot make heads or tails of this one. haha. I might want to just go back to the procedural way of doing myslqi. haha.

t_thakar, as stated above, it does echo 1 since i am the first user.

Hello patk570,

We have (almost) no idea from your code what $this->db_connection actually is.

You keep talking about PDO but the var_dump and print_r that you provided say it clearly: object(mysqli_stmt) .

So is it mysqli and NOT PDO ?

If your current value is the same as your new value no update happens