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
[edit] Last updated: Fri, 25 May 2012

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

クオートするスタイルを変更するため、 ドライバにデータ型のヒントを提供します。

返り値

理論上安全なクオートされた 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'

参考



PDO::rollBack> <PDO::query
[edit] Last updated: Fri, 25 May 2012
 
add a note add a note User Contributed Notes PDO::quote
orrd101 at gmail dot com 30-Apr-2012 01:15
Despite the recommendation above that you are "strongly recommended" to use PDO::prepare(), using PDO::quote() with PDO::query() is significantly faster than using prepared statements (unless you're submitting many identical queries in a sequence).  Test data from several sources shows about a 2-3x speed penalty for using prepared queries if you are only submitting a single query, and prepared queries only have a speed advantage when you are submitting thousands of queries from prepared statement at once.

It's also more convenient since less lines of code are required and you don't have to deal with the challenge of keeping all your ?'s properly ordered to match a separate parameter list as you do with prepared statements.

So don't let the wording of the description scare you away from using this method.
hungry dot rahly at gmail dot com 27-Sep-2010 09:31
For those of you who do want to have null returned as NULL, it is fairly easy to add.

<?php
class Real_PDO extends PDO {
  public function
quote($value, $parameter_type = PDO::PARAM_STR ) {
    if(
is_null($value) ) {
      return
"NULL";
    }
    return
parent::quote($value, $parameter_type);
  }
}
?>

then all you have to do is change your creation of the PDO object to this new class, and you shouldn't have to change any other function calls.  The nice thing about this method is that you can override any PDO function or add your own.  IMO, PDO should natively handle this, as there is a difference in databases between NULL and an empty string(''), the quoting of numeric values is another issue altogether.
php at deobald dot org 20-May-2008 08: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".

 
show source | credits | sitemap | contact | advertising | mirror sites