PHP 8.3.4 Released!

pg_update

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

pg_update テーブルを更新する

説明

pg_update(
    PgSql\Connection $connection,
    string $table_name,
    array $values,
    array $conditions,
    int $flags = PGSQL_DML_EXEC
): string|bool

pg_update() は、 conditions にマッチするレコードを values に更新します。

flags が指定された場合、指定したオプションとともに pg_convert()data に適用されます。

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

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

パラメータ

connection

PgSql\Connection クラスのインスタンス。

table_name

行を更新するテーブルの名前。

values

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

conditions

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

flags

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

戻り値

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

変更履歴

バージョン 説明
8.1.0 connection は、PgSql\Connection クラスのインスタンスを期待するようになりました。 これより前のバージョンでは、リソース を期待していました。

例1 pg_update() の例

<?php
$db
= pg_connect('dbname=foo');
$data = array('field1'=>'AA', 'field2'=>'BB');

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

参考

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

add a note

User Contributed Notes 3 notes

up
2
dominik dot bonsch at homesono dot de
16 years ago
Using pg_update() and pg_insert() without key validation is not secure!

You need to check which data pairs you get, and if you want to allow to updated this column.

Example:

You have a table with tree colums: username, password, userlevel.

Your users may change only their username, and their password but not their userlevel.

If you don't filter the keys in the request array, every user can now change his userlevel just by sending a POST Request with "userlevel=>100".

So if you don't check if the key are allowed in your request array you'll get serious sql injection vulnarabilities in your code.
up
-2
jhooks
17 years ago
> Return Values
>
> Returns TRUE on success or FALSE on failure. Returns string if
> PGSQL_DML_STRING is passed via options.

I have found in my copy of PHP (version 4.4.0) that if you use the 'PGSQL_DML_STRING' option, the function does not execute any query. It merely returns the query which would have been executed.

Another thing I noticed, pg_update does not seem to make use of pg_trace (atleast in 4.4.0).

PS this isn't a bug report, just an explanation of some undocumented features I noticed. As the manual says, the function is still in development so this behaviour may differ from version to version.
up
-4
sdibb at myway dot com
18 years ago
This function is similar to PEAR::DB's autoExecute() function, with the only difference being that the where clause is an array instead of a string.

Also, if you want to use your instance of the DB class with this function, you can reference the existing resource connection with $db->connection.

An example would be:
<?
pg_update($db->connection, $arr_update, $arr_where);
?>
To Top