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

search for in the

PDO::rollBack> <PDO::query
Last updated: Fri, 06 Nov 2009

view this page in

PDO::quote

(PHP 5 >= 5.1.0, PECL pdo >= 0.2.1)

PDO::quote クエリ用の文字列をクオートする

説明

string PDO::quote ( string $string [, int $parameter_type = PDO::PARAM_STR ] )

PDO::quote() は入力文字列のまわりに引用符を付け (必要であれば) 、 入力文字列にあるシングルクオートをエスケープします。その場合、 構成しているドライバに適したクオート形式が使用されます。

この関数を SQL の構築に使用する場合、 SQL ステートメントにユーザーの入力値を埋め込むための PDO::quote() を使用する代わりに、 バインドパラメータを用いて SQL を準備するための PDO::prepare() を使用することが強く推奨されます。 バインドパラメータを用いるプリペアドステートメントは、 補間されたクエリした場合に比べ、 移植性や利便性に優れ、SQL インジェクションに対する免疫力を持つだけでなく しばしばより高速で、サーバやクライアント側でコンパイル済みの形式でクエリを キャッシュする事が可能です。

全ての PDO ドライバがこのメソッドを実装しているわけではありません (たとえば PDO_ODBC などの例があります)。 代わりにプリペアドステートメントを使用することを検討してください。

パラメータ

string

クオートされる文字列を指定します。

parameter_type

クオートするスタイルを変更するため、 ドライバにデータ型のヒントを提供します。 デフォルト値は、PDO::PARAM_STR です。

返り値

理論上安全なクオートされた SQL ステートメントの文字列を返します。 ドライバがこの方法での引用符付けをサポートしていない場合は FALSE を返します。

例1 通常の文字列をクオートする

<?php
$conn 
= new PDO('sqlite:/home/lynn/music.sql3');

/* 単純な文字列 */
$string 'Nice';
print 
"Unquoted string: $string\n";
print 
"Quoted string: " $conn->quote($string) . "\n";
?>

上の例の出力は以下となります。

Unquoted string: Nice
Quoted string: 'Nice'

例2 危険な文字列をクオートする

<?php
$conn 
= new PDO('sqlite:/home/lynn/music.sql3');

/* 危険な文字列 */
$string 'Naughty \' string';
print 
"Unquoted string: $string\n";
print 
"Quoted string:" $conn->quote($string) . "\n";
?>

上の例の出力は以下となります。

Unquoted string: Naughty ' string
Quoted string: 'Naughty '' string'

例3 複雑な文字列をクオートする

<?php
$conn 
= new PDO('sqlite:/home/lynn/music.sql3');

/* 複雑な文字列 */
$string "Co'mpl''ex \"st'\"ring";
print 
"Unquoted string: $string\n";
print 
"Quoted string: " $conn->quote($string) . "\n";
?>

上の例の出力は以下となります。

Unquoted string: Co'mpl''ex "st'"ring
Quoted string: 'Co''mpl''''ex "st''"ring'

参考



add a note add a note User Contributed Notes
PDO::quote
the last atlante
15-Aug-2009 07:27
For all ex-users of "mysql_real_escape_string()", contrary to this function "PDO::quote" escapes the characters '_' et '%' too.

So, no need to escape  this characters yourself !!!
php at deobald dot org
21-May-2008 12:33
Note that this function just does what the documentation says: It escapes special characters in strings.

It does NOT - however - detect a "NULL" value. If the value you try to quote is "NULL" it will return the same value as when you process an empty string (-> ''), not the text "NULL".

PDO::rollBack> <PDO::query
Last updated: Fri, 06 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites