pg_select

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

pg_select レコードを選択する

説明

pg_select(
    resource $connection,
    string $table_name,
    array $assoc_array,
    int $options = PGSQL_DML_EXEC,
    int $result_type = PGSQL_ASSOC
): mixed

pg_select() は、field=>value 形式の assoc_array で指定したレコードを選択します。 クエリに成功した場合、assoc_array で指定した条件に マッチする全てのレコードとフィールドを含む配列が返されます。

options が指定された場合、 指定したフラグとともに pg_convert()assoc_array に適用されます。

pg_update() は生の値を渡します。 値はエスケープするか、PGSQL_DML_ESCAPE オプションを指定しなければいけません。 PGSQL_DML_ESCAPE はパラメータや識別子をクォートし、エスケープします。 よって、テーブル/カラム名は大文字小文字を区別します。

エスケープやプリペアドクエリであっても、 LIKE, JSON, Array, Regex などのクエリを守れない可能性があることに注意してください。 これらのパラメータはコンテクストに応じて処理されるべきです。 たとえば、値をエスケープ/検証する処理を行うことなどです。

パラメータ

connection

PostgreSQL データベースの接続リソース。

table_name

行を選択するテーブルの名前。

assoc_array

テーブル table_name のフィールド名をキーに、 そして取得対象となる行にマッチするデータを値にもつ配列。

options

PGSQL_CONV_FORCE_NULLPGSQL_DML_NO_CONVPGSQL_DML_ESCAPEPGSQL_DML_EXECPGSQL_DML_ASYNC あるいは PGSQL_DML_STRING の組み合わせ。 options の一部に PGSQL_DML_STRING が含まれていた場合、 クエリ文字列が返されます。 PGSQL_DML_NO_CONV あるいは PGSQL_DML_ESCAPE が設定されている場合は、内部的に pg_convert() を呼びません。

返り値

成功した場合に true を、失敗した場合に false を返します。 optionsPGSQL_DML_STRING が渡された場合は文字列を返します。

変更履歴

バージョン 説明
7.1.0 result_type パラメータが追加されました。

例1 pg_select() の例

<?php 
  $db 
pg_connect('dbname=foo');
  
// これは少しだけ安全です。なぜなら、全ての値がエスケープされるからです。
  // しかし、PostgreSQL は JSON/Array をサポートしています。これらの型に
  // ついてはエスケープされたクエリや、プリペアドクエリでも安全ではありません。
  
$rec pg_select($db'post_log'$_POST);
  if (
$rec) {
      echo 
"選択されたレコード:\n";
      
var_dump($rec);
  } else {
      echo 
"ユーザーが誤った入力を送信しました。\n";
  }
?>

参考

  • pg_convert() - 連想配列の値を、SQL 文として実行可能な形式に変換する

add a note add a note

User Contributed Notes 2 notes

up
2
david dot tulloh at infaze dot com dot au
18 years ago
Valid options are PGSQL_DML_NO_CONV, PGSQL_DML_EXEC, PGSQL_DMP_ASYNC, PGSQL_DML_STRING (pulled out of source code).

This function does not support selecting from multiple tables.  You can get around this by setting the PGSQL_DML_NO_CONV option.  This prevents the error which occurs when the function tries to convert the condition array.

I think it is also important to point out that the table_name field is not safe, particularily with the PGSQL_DML_NO_CONV option.

The arguements array field is compulsory, as documented.  What isn't so clear is that the array has to actually have some values in it, you can't do a select all.

In summary, this function is good for a very small subset of basic queries.  If you are after anything more complex you are better off with pg_query.
up
-4
wietse at cj2 dot nl
18 years ago
David mentioned that you can't do a Select all.
However, when executing this script:
<?php
    $conn_string
= "dbname=mydb";
   
$db = pg_connect($conn_string);
   
$selectfields = array("imgid" => "");
   
$records = pg_select($db,"mmsfiles",$selectfields);
   
print_r($records);
?>
...I get this result:
Array
(
    [0] => Array
        (
            [imgid] => 1
            [file] => /home/wietse/public_html/mms/images/1.gif
            [thumb] =>
        )
    [1] => Array
        (
            [imgid] => 2
            [file] => /home/wietse/public_html/mms/images/2.gif
            [thumb] =>
        )
    [2] => Array
        (
            [imgid] => 3
            [file] => /home/wietse/public_html/mms/images/3.gif
            [thumb] =>
        )
    [3] => Array
        (
            [imgid] => 4
            [file] => /home/wietse/public_html/mms/images/4.gif
            [thumb] =>
        )
)
To Top