forrest79 / phpgsql

Simple and fast PHP database library for PostgreSQL with auto converting DB types to PHP and fluent interface for SQL query writing.
Other
11 stars 3 forks source link

Error with timestamp type #25

Closed RonEskinder closed 3 years ago

RonEskinder commented 3 years ago

Im getting this error while fetching a timestamp column, any ideas why?

$connection = new Connection("host=" . DB_HOST . " port=5432 dbname=" . DB_DATA . " user=" . DB_USER . " password=" . DB_PASS . " connect_timeout=5");
$connection->connect();

$result = $connection->query("SELECT * FROM tc_mail WHERE status = 0 and \"dateSent\" is null;");
$result = $result->fetchAll();

foreach ($result as $row) {
    $id = $row["id"];
    $mailSubject = $row["mailSubject"];
    $mailType = $row["mailType"];
    $body = $row["body"];
    $dateCreated = $row["dateCreated"]; <-- This value is '2020-09-09 15:57:01' and breaks
}
PHP Fatal error:  Uncaught Forrest79\PhPgSql\Db\Exceptions\DataTypeParserException: 
Can't convert value 'Y-m-d H:i:s' to datetime with format '2020-09-09 16:19:02.095861'. 
in /var/autosmart/daemon/vendor/forrest79/phpgsql/src/Db/Exceptions/DataTypeParserException.php:27

php version PHP 7.2.31 postgres version 12.2

forrest79 commented 3 years ago

Hi, thanks for a report. Can you show me tc_mail.dateSend column definition? I tried to prepare a fix, it's in the fix-timestamp-ms branch, can you try it?

RonEskinder commented 3 years ago

This is the whole table, you can see the column dateSent as a timestamp type

CREATE TABLE public.tc_mail (
    id serial NOT NULL,
    userid int4 NOT NULL,
    "mailAddress" text NULL,
    "mailSubject" varchar(256) NULL,
    "mailType" varchar(128) NULL,
    "dateCreated" timestamp NOT NULL DEFAULT now(),
    "dateSent" timestamp NULL,
    body text NULL,
    extras varchar(5000) NULL,
    status int4 NULL DEFAULT 0,
    CONSTRAINT tc_mail_pkey PRIMARY KEY (id)
);
RonEskinder commented 3 years ago

branch fix-timestamp-ms is working fine

forrest79 commented 3 years ago

Thanks, I merged fix to the master and tag a new version for composer.