note 115063 added to function.pg-query-params
| From: | php at richardneill dot org | Date: | Tue, 20 May 2014 16:35:35 +0000 |
| Subject: | note 115063 added to function.pg-query-params | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199582@lists.php.net to get a copy of this message | ||
A note on type-juggling of booleans:
pg_query_params() and friends do seamless, automatic conversion between PHP-NULL and SQL-NULL and
back again, where appropriate.
Hoever, everything else goes in (and comes out) as a string.
The following approach may be helpful when handling boolean fields:
<?php
$sql = " ... ";
$params = array (1, 2, 3, true, false);
//Convert booleans to 'true' and 'false'. [NULLS are already handled].
foreach ($params as &$value){
if (is_bool($value)){
$value = ($value) ? 'true':'false';
}
}
//Now do the query:
$result = pg_query_params ($sql, $params);
$row = pg_fetch_assoc ($result,0) //first row
//For booleans, convert 't' and 'f' back to true and false. Check the column
type so we don't accidentally convert the wrong thing.
foreach ($row as $key => &$value){
$type = pg_field_type($result,pg_field_num($result, $key));
if ($type == 'bool'){
$value = ($value == 't');
}
}
//$row[] now contains booleans, NULLS, and strings.
?>
----
Server IP: 72.52.91.14
Probable Submitter: 82.10.237.122
----
Manual Page -- http://php.net/manual/en/function.pg-query-params.php
Edit -- https://master.php.net/note/edit/115063
Del: integrated -- https://master.php.net/note/delete/115063/integrated
Del: useless -- https://master.php.net/note/delete/115063/useless
Del: bad code -- https://master.php.net/note/delete/115063/bad+code
Del: spam -- https://master.php.net/note/delete/115063/spam
Del: non-english -- https://master.php.net/note/delete/115063/non-english
Del: in docs -- https://master.php.net/note/delete/115063/in+docs
Del: other reasons-- https://master.php.net/note/delete/115063
Reject -- https://master.php.net/note/reject/115063
Search -- https://master.php.net/manage/user-notes.php