Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO bindParam Timestamp is not inserted

Tags:

php

pdo

I have following PHP code for inserting session data into a table called ds_session:

    // session does not exists create insert statement
    $insertSQL = 'INSERT INTO ds_session (session_id, user_agent, session_expire, date_created, session_data)
                VALUES (:SESSION_ID, :USER_AGENT, :SESSION_EXPIRE, :DATE_CREATED, :SESSION_DATA)';

    $insertStmt = $this->pdo->prepare($insertSQL);
    $insertStmt->bindParam(':SESSION_ID', $id);
    $insertStmt->bindParam(':USER_AGENT', $_SERVER['HTTP_USER_AGENT']);
    $insertStmt->bindParam(':SESSION_EXPIRE', $time);
    $insertStmt->bindParam(':DATE_CREATED', time());
    $insertStmt->bindParam(':SESSION_DATA', $sessData);
    $insertResult = $insertStmt->execute();

My problem here is, that the params SESSION_EXPIRE and DATE_CREATED are not set. The table definition looks like this:

CREATE TABLE `ds_session` (
  `session_id` varchar(32) NOT NULL default '',
  `user_agent` varchar(255) NOT NULL default '',
  `session_expire` datetime NOT NULL,
  `date_created` datetime NOT NULL,
  `session_data` longtext,
PRIMARY KEY  (`session_id`),
KEY `session_expire` (`session_expire`)
) ENGINE=MyISAM

What is the problem here? Am I doing something wrong with the PDOStatement?

like image 352
Georg Leber Avatar asked Nov 29 '25 23:11

Georg Leber


1 Answers

datetime values are expected as a string, not a number. Use FROM_UNIXTIME to convert:

$insertSQL = 'INSERT INTO ds_session (session_id, user_agent, session_expire,
                    date_created, session_data)
            VALUES (:SESSION_ID, :USER_AGENT, FROM_UNIXTIME(:SESSION_EXPIRE),
                    FROM_UNIXTIME(:DATE_CREATED), :SESSION_DATA)';
like image 165
phihag Avatar answered Dec 01 '25 13:12

phihag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!