Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: pg_query_params - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

pg_query" width="11" height="7"/> <pg_put_line
Last updated: Fri, 30 May 2008

view this page in

pg_query_params

(PHP 5 >= 5.1.0)

pg_query_params — SQL コマンドとパラメータを分割してサーバにを送信し、その結果を待つ

説明

resource pg_query_params ( resource $connection , string $query , array $params )
resource pg_query_params ( string $query , array $params )

コマンドをサーバに送信し、その結果を待ちます。パラメータを SQL コマンド とは別に渡すことが可能です。

pg_query_params()pg_query() と似ていますが、追加の機能を有しています。それはパラメータ値が コマンド文字列と分離しているということです。 pg_query_params() は PostgreSQL 7.4 以降の接続でのみ サポートされます。それ以前のバージョンでは失敗します。

パラメータを使用する際は、query 文字列内で $1、$2 のように参照されます。params で 実際の値を指定します。NULL を指定すると、SQL の NULL とみなされます。

pg_query() に対する pg_query_params() の最大の利点は、パラメータの値を query 文字列から 分離できることです。そのため、退屈でエラーの元となりやすいクォート・ エスケープなどをしなくてもよくなります。pg_query() と異なり、pg_query_params() ではひとつの SQL コマンドしか実行できません(クエリ文字列にセミコロンを含めることは 可能です。しかしそれ以降にコマンドを続けることはできません)。

パラメータ

connection

PostgreSQL データベース接続リソース。connection が指定されていない場合はデフォルトの接続が使用されます。 デフォルトの接続は、直近の pg_connect() あるいは pg_pconnect() によって作成されたものです。

query

パラメータ化した SQL 文。ひとつの文のみである必要があります (複数の文をセミコロンで区切る形式は使用できません)。パラメータを 使用する際は $1、$2 などの形式で参照されます。

params

プリペアドステートメント中の $1、$2 などのプレースホルダを 置き換えるパラメータの配列。配列の要素数はプレースホルダの 数と一致する必要があります。

返り値

成功した場合にクエリ結果リソース、失敗した場合に FALSE を返します。

例1 pg_query_params() の使用法

<?php
// "mary"という名前のデータベースに接続
$dbconn pg_connect("dbname=mary");

// Joe's Widgets という名前の店を探す。"Joe's Widgets" を
// エスケープする必要がないことに注意
$result pg_query_params($dbconn'SELECT * FROM shops WHERE name = $1', array("Joe's Widgets"));

// pg_query を使用した場合と比較
$str pg_escape_string("Joe's Widgets");
$result pg_query($dbconn"SELECT * FROM shops WHERE name = '{$str}'");

?>

参考



pg_query" width="11" height="7"/> <pg_put_line
Last updated: Fri, 30 May 2008
 
add a note add a note User Contributed Notes
pg_query_params
jsnell at e-normous dot com
27-Sep-2007 07:57
When inserting into a pg column of type bool, you cannot supply a PHP type of bool.  You must instead use a string "t" or "f". PHP attempts to change boolean values supplied as parameters to strings, and then attempts to use a blank string for false.

Example of Failure:
pg_query_params('insert into table1 (bool_column) values ($1)', array(false));

Works:
pg_query_params('insert into lookup_permissions (system) values ($1)', array(false ? 't' : 'f'));
dt309 at f2s dot com
22-Dec-2006 05:11
If you need to provide multiple possible values for a field in a select query, then the following will help.

<?php
// Assume that $values[] is an array containing the values you are interested in.
$values = array(1, 4, 5, 8);

// To select a variable number of arguments using pg_query() you can use:
$valuelist = implode(', ', $values);
$query = "SELECT * FROM table1 WHERE col1 IN ($valuelist)";
$result = pg_query($query)
    or die(
pg_last_error());

// You may therefore assume that the following will work.
$query = 'SELECT * FROM table1 WHERE col1 IN ($1)';
$result = pg_query_params($query, array($valuelist))
    or die(
pg_last_error());
// Produces error message: 'ERROR: invalid input syntax for integer'
// It only works when a SINGLE value specified.

// Instead you must use the following approach:
$valuelist = '{' . implode(', ', $values . '}'
$query = 'SELECT * FROM table1 WHERE col1 = ANY ($1)';
$result = pg_query_params($query, array($valuelist));
?>

The error produced in this example is generated by PostGreSQL.

The last method works by creating a SQL array containing the desired values. 'IN (...)' and ' = ANY (...)' are equivalent, but ANY is for working with arrays, and IN is for working with simple lists.
mledford
05-Oct-2006 12:18
If you are trying to replicate the function pg_query_params, you might also want to support NULL values. While is_int returns true for a NULL value, the formatting for the SQL.

function pg_query_params( $db, $query, $parameters ) {
    // Escape parameters as required & build parameters for callback function
    global $pg_query_params__parameters;
    foreach( $parameters as $k=>$v ) {
        if ( is_null($v) ) {
            $parameters[$k] = 'NULL';
        } else {
            $parameters[$k] = ( is_int( $v ) ? $v : "'".pg_escape_string( $v )."'" );
        }
    }
    $pg_query_params__parameters = $parameters;
       
    // Call using pg_query
    return pg_query( $db, preg_replace_callback( '/\$([0-9]+)/', 'pg_query_params__callback', $query));
}
cc+php at c2se dot com
02-Sep-2006 09:17
This is a useful function for preventing SQL injection attacks, so, for those of us who are not yet able to upgrade to PHP5.1, here is a replacement function which works similarly on older versions of PHP...

<?php   # Parameterised query implementation for Postgresql and older versions of PHP

       
if( !function_exists( 'pg_query_params' ) ) {

                function
pg_query_params__callback( $at ) {
                        global
$pg_query_params__parameters;
                        return
$pg_query_params__parameters[ $at[1]-1 ];
                }

                function
pg_query_params( $db, $query, $parameters ) {

                       
// Escape parameters as required & build parameters for callback function
                       
global $pg_query_params__parameters;
                        foreach(
$parameters as $k=>$v )
                               
$parameters[$k] = ( is_int( $v ) ? $v : "'".pg_escape_string( $v )."'" );
                       
$pg_query_params__parameters = $parameters;

                       
// Call using pg_query
                       
return pg_query( $db, preg_replace_callback( '/\$([0-9]+)/', 'pg_query_params__callback', $query ) );

                }
        }

       
// Example: pg_query_params( $db_resource, "SELECT * FROM table WHERE col1=$1 AND col2=$2", array( 42, "It's ok" ) );
?>

pg_query" width="11" height="7"/> <pg_put_line
Last updated: Fri, 30 May 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites