How do you compile integer data in Postgres by PHP?
My integers are string as the following show by var_dump ( $_SESSION )
'logged_in' => int 1
'user_id' => string '9' (length=1) // should be int
'a_moderator' => string '0' (length=1) // should be int
I compile the values by the following code which obviously is the source of the problem after pg_prepare
and pg_execute
.
while ( $row = pg_fetch_array( $result ) ) {
$user_id = $row['user_id'];
}
You can convert strings to integers using intval:
$str = "5";
$num = intval($str);
or by type coercion:
$str = "5";
$num = (int) $str;
//this works too
$num = (integer) $str;
some test code:
<?php
$str = "10";
$num = intval($str);
//$num = (int)$str;
if ($str === 10) echo "String";
if ($num === 10) echo "Integer";
?>
Further reading:
It's ordinary for PHP database extensions to return all data types as string, because some SQL data types cannot be represented in PHP native data types.
Example: PostgreSQL BIGINT
(8-byte integer data type) would overflow the int
PHP type (unless PHP is compiled with 64-bit support), and be cast as float
which might lose exactness.