1

i have the following request:

 select * from newagenda where 
     debut >'$1' AND debut < date '$1' + interval '24 hours' and
     agendaid=$2' 

which fails with a type cast error...

 failed: the error: = FEHLER: ungültige Eingabesyntax für Typ timestamp:

i try to translate: ERROR: invalid inputsyntax for type timestamp:

before i parametrized, i had

 select * from newagenda where debut >'$adate' AND debut < date '$adate' +
     interval '24 hours' and agendaid=$agid;

which worked perfectly well.....

now, the date comes back through a post request from a web request, so can inherently be manipulated, that's why would like to parametrize it, but i am clueless on how to get this to work.....

i tried

 select * from newagenda where 
     debut >'$1'::DATE AND debut < date '$1'::DATE + interval '24 hours' and
     agendaid=$2' 

or

$largs = array($isodate."::DATE");

also

$largs = array("'".$isodate."'::DATE");

but nothing worked...... how can i get this get to work? thanks in advance!

6
  • Can you translate the error message? Commented Mar 19, 2019 at 15:06
  • The first query has an obvious syntax error (typo here?) but even without, that is not a parametrized query... Commented Mar 19, 2019 at 15:08
  • could you please point out the obvious error? Commented Mar 19, 2019 at 15:11
  • Can you not use PDO ? Commented Mar 19, 2019 at 15:17
  • and the proper SQL format for a date/time is date('Y-m-d G:i:s') Commented Mar 19, 2019 at 15:20

2 Answers 2

1

You are probably looking for something along the lines of :

<?php

//using single quoates to make sure the variables aren't expanded
$sql = 'select * from newagenda 
        where 
            debut >$1::DATE 
            AND debut < date $2::DATE
            and agendaid=$3';

$formattedDate = new \DateTime($date);
$rangeStart = $formattedDate->format('Y-m-d G:i:s');
$formattedDate->add('1 day');
$rangeEnd = $formattedDate->format('Y-m-d G:i:s');

pg_prepare($con,'sel_from_agenda', $sql);
pg_execute($con,'sel_from_agenda', [$rangeStart, $rangeEnd, $agid]);

I using functional connections here but it would probably be best to use OO or PDO.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, that works, even without the casts, but this means i can't use the postgres functions ?
Why wouldn't you be able to ? pg_prepare($con,'sel_my_func', 'select myFumction($1, $2)');
Ah i meant the date manipulations directly in the sql request
I see, in that case, have a look at stackoverflow.com/questions/7796657/…
0

You can use the pomm-project/foundation library take advantage of the parameters (and results) converters and by the way use the range type and operators:

$pomm = new PommProject\Foundation\Pomm(['db' => ['dsn' => 'pgsql://user@host/db_name']]);

$date = new \Datetime(); // set your date here
$query = <<<SQL
select * from newagenda
where
  tsrange($*::timestamp, $*::timestamp + '1 day'::interval, '()') @> debut
  and agendaid = $*
SQL;
$iterator = $pomm['db']
    ->getQueryManager()
    ->query($query, [$date, $date, 123]);

foreach ($iterator as $row) {
    print_r($row);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.