PHP Doku:: Quotes a string for use in a query. - pdo.quote.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDatenbankerweiterungenAbstraktionsebenenPHP Data ObjectsDie PDO-KlassePDO::quote

Ein Service von Reinhard Neidl - Webprogrammierung.

Die PDO-Klasse

<<PDO::query

PDO::rollBack>>

PDO::quote

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

PDO::quote Quotes a string for use in a query.

Beschreibung

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

PDO::quote() places quotes around the input string (if required) and escapes special characters within the input string, using a quoting style appropriate to the underlying driver.

If you are using this function to build SQL statements, you are strongly recommended to use PDO::prepare() to prepare SQL statements with bound parameters instead of using PDO::quote() to interpolate user input into an SQL statement. Prepared statements with bound parameters are not only more portable, more convenient, immune to SQL injection, but are often much faster to execute than interpolated queries, as both the server and client side can cache a compiled form of the query.

Not all PDO drivers implement this method (notably PDO_ODBC). Consider using prepared statements instead.

Parameter-Liste

string

The string to be quoted.

parameter_type

Provides a data type hint for drivers that have alternate quoting styles.

Rückgabewerte

Returns a quoted string that is theoretically safe to pass into an SQL statement. Returns FALSE if the driver does not support quoting in this way.

Beispiele

Beispiel #1 Quoting a normal string

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

/* Simple string */
$string 'Nice';
print 
"Unquoted string: $string\n";
print 
"Quoted string: " $conn->quote($string) . "\n";
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

Unquoted string: Nice
Quoted string: 'Nice'

Beispiel #2 Quoting a dangerous string

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

/* Dangerous string */
$string 'Naughty \' string';
print 
"Unquoted string: $string\n";
print 
"Quoted string:" $conn->quote($string) . "\n";
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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

Beispiel #3 Quoting a complex string

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

/* Complex string */
$string "Co'mpl''ex \"st'\"ring";
print 
"Unquoted string: $string\n";
print 
"Quoted string: " $conn->quote($string) . "\n";
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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

Siehe auch


3 BenutzerBeiträge:
- Beiträge aktualisieren...
hungry dot rahly at gmail dot com
27.09.2010 18: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.
the last atlante
15.08.2009 0: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
20.05.2008 17: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".



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)