0

The php code is bellow.

$cc='create table mydataI(net) as VALUES (-20.12::double precision)';
$db->query($cc) or die('<br><br><br> pos02:'.$db->lasterror.'<br>'.nl2br($cc));

The error is as follows:

pos02:ERROR: 42601: syntax error at or near "precision" LINE 1: ...e table mydataI(net) as VALUES (-20.12::numeric precision) ^ LOCATION: scanner_yyerror, scan.l:1081 create table mydataI(net) as VALUES (-20.12::double precision)

if I use instead:

$cc='create table mydataI(net) as VALUES (-20.12)';
$db->query($cc) or die('<br><br><br> pos02:'.$db->lasterror.'<br>'.nl2br($cc));

The php code runs correctly

When I copy paste the initial string to phpAdmin4 the code is run correctly by the postgres interpreter.

I tried to using php the code:

$cc='create table mydataI(net) as VALUES (-20.12::double),-15.01);';
$db->query($cc) or die('<br><br><br>     

And this works ( it seems that the php "pipe" somehow interprets and converts some parts - in this case erroneously ).

The full code that I use has a file 'mydataQuery.php' that connects the $db to the database.

    <?php 
require 'mydataQuery.php'; 
    global $db; //the database is connected from other code 
    $cc="drop table if exists mydataI cascade;"; 
    $db->query($cc) or die('<br><br><br> pos01:'.$db->lasterror.'<br>'.$cc); echo "Dropped table<br>"; $cc="create table mydataI(aa) as VALUES (1.1 double precision),(1.2::double precision);"; $db->query($cc) or die('<br><br><br> pos02:'.$db->lasterror.'<br>'.nl2br($cc)); echo "Created table<br>"; ?> 
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
George Kourtis
  • 2,381
  • 3
  • 18
  • 28
  • @FrankHeikens The create value syntax is correct and works as expected in the postresql interpreter. The problem shows up when used via PHP. – George Kourtis Aug 19 '22 at 13:40
  • Didn't know that, learned something new! What happens when you use float8 instead of double precision? And something else, are you sure about the datatype? It's not precise. – Frank Heikens Aug 19 '22 at 13:54
  • @FrankHeikens I tried float8 the answer is 'type "numeric8" does not exist LINE 1:' ( I wrote float8 that I tested does indeed work in the pgamdin4 interpreter ). – George Kourtis Aug 19 '22 at 14:02
  • What php-version and drivers do you use? – Frank Heikens Aug 19 '22 at 14:06
  • I am starting to suppose that the php "pipe" tries to transform the sql statment in a way to allow for the same input syntax irrespectivelly from the backend database ( mysql or postgres). – George Kourtis Aug 19 '22 at 14:13
  • @FrankHeikens . The php version is a little old I suppose (as the server is old). Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies – George Kourtis Aug 19 '22 at 14:15
  • That would be really strange, because there are dozens of other databases as well. Could you share the complete code? Also how you create a database connection, etc. – Frank Heikens Aug 19 '22 at 14:16
  • @FrankHeikens The code is bellow, the file "mydataQuery.php' connects the $db to the database. query($cc) or die('


    pos01:'.$db->lasterror.'
    '.$cc); echo "Dropped table
    "; $cc="create table mydataI(aa) as VALUES (1.1 double precision),(1.2::double precision);"; $db->query($cc) or die('


    pos02:'.$db->lasterror.'
    '.nl2br($cc)); echo "Created table
    "; ?>
    – George Kourtis Aug 19 '22 at 14:28
  • The `^` symbol should point to the exact part of the query where the parse error happens, but your output is misaligned. What's the underlying database extension? – Álvaro González Aug 19 '22 at 17:07
  • @Álvaro González What you mean by database extension? I work inside dolibarr and I don't even know how the $db is initialized – George Kourtis Aug 19 '22 at 21:45
  • The PHP "pipe" to the postgress interpreter converts "double precision" to something like "numeric precision". – George Kourtis Aug 19 '22 at 21:50
  • Well, I mean [literally that](https://www.php.net/manual/en/refs.database.php). If it isn't your own code but some third-party software, it's okay, but that's relevant information that should have been mentioned in the question. I peeked at the source code and it appears to use [pg_...()](https://www.php.net/manual/en/book.pgsql.php) functions, but it's a very old codebase that does lots of magic. It's hard to say what the problem is without a step debugger. – Álvaro González Aug 21 '22 at 10:33
  • @ÁlvaroGonzález You are totally correct !!! ... but I didn't knew about it ... ( sorry I am a newbie in the subject ). I used a "clean" postgres and everything works. Probably the "dolibarr" connection does some code mangling in order to adapt mysql to postgres that I couldn't imagine of, and it is able to do it behind the call to pg_query ( this is exactly what I could't imagine of). As soon as I have some more information I will revert back. I was just supposing that "pg_query" would go direct to postgres but that is not always the case ... as I painfully found out. – George Kourtis Aug 21 '22 at 20:26

0 Answers0