Pg_Query Result Contains Strings Instead of Integer, Numeric

pg_query result contains strings instead of integer, numeric

That's what PHP does. From the manual:

Each value in the array is represented as a string.

PostgreSQL integer field is returned as string in PHP

I searched a lot i found this problem is because of php does not support bigint in 32bit machine if your machine is x64 so you should check is your php x64 too or not:
you can check it by following script:

echo (PHP_INT_SIZE === 8) ? "64 bit " : "32 bit ";

this problem is not PDO issue and you cann't solve it with changing PDO options it's because of pgsql driver. pgsql return bigint as string if the used language doesn't have any type for bigint.

Why is pg_query_params reading a boolean value as a string?

Let's face it:

  • Data returned by either PDO or the pgsql libraries are always strings.
  • Postgresql representation for booleans are strings 't' and 'f'.

This is specified by the PHP documentation in the "returned values" of the different fetch functions. here and here.

If you want these results to be cast to PHP equivalent types, you must set up a converter system.

Getting boolean values from PostgreSQL

No.

If you're using booleans while inserting or updating data, CodeIgniter will convert them, but it can't do that while fetching records because it has no knowledge of the data types it reads - it's a Query Builder, not an ORM.

What you can do is create a bunch of classes to represent your specific result sets and use custom_result_object() or custom_row_object() when fetching.

Obviously, the 't'/'f' to true/false conversion would have to be performed by these custom-made objects.

How to use PHP array in Postgres select

Strings needs single quotes as I know. Concat the elements of array like this:

$data = ['a','b','c','d'];
$x = "'" . implode("','", $data) . "'";
var_dump($x);

Result:

'a','b','c','d'

Warning: pg_query(): Query failed: ERROR: syntax error at or near

The correct syntax for the SQL statement is as follows:

'SELECT * FROM public."tblProjects" WHERE "tblProjects"."ProjectID"=' . $pid;

The main reason for this not working in the first place was because of mixed cases in column and table names.

See this answer to a similar question: https://stackoverflow.com/a/12250721/3620249

I had thought the $pid needed to be within the query string (single quotes). However, the variable would not be called unless the query string was double quoted. It then became difficult to manage the mixed cases in the column/table names with quotes as well, so I tried using + to concatenate instead of . as you can see in my question.

Lessons learned:

  1. Concatenate in PHP using .
  2. Variables can be called from outside of a query string using concatenation.
  3. If table/column names contain mixed cases, they must be contained within double quotations


Related Topics



Leave a reply



Submit