PHP pdo_quote function
Like this blog? Consider exploring one of our sponsored banner ads...
PDO->quote()
(no version information, might be only in CVS)
PDO->quote() —Quotes a string for use in a query.
Descriptionclass PDO {
string quote ( string string [, int parameter_type] )
}
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 a 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.
string
The string to be quoted.
Provides a data type hint for drivers that have alternate quoting styles.
The default value is PDO::PARAM_STR.
Return Values
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.
Example 1. Quoting a normal string
$conn = new PDO('sqlite:/home/lynn/music.sql3'); /* Simple string */ $string = 'Nice'; print "Unquoted string: $string\n"; print "Quoted string: " . $conn->quote($string) . "\n";
The above example will output:
Unquoted string: NiceQuoted string: ‘Nice’
Example 2. Quoting a dangerous string
$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";
The above example will output:
Unquoted string: Naughty ‘ stringQuoted string: ‘Naughty ” string’
Example 3. Quoting a complex string
$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";
The above example will output:
Unquoted string: Co’mpl”ex “st’”ringQuoted string: ‘Co”mpl””ex “st””ring’
See Also
PDO->prepare()PDOStatement->execute()
About this entry
You’re currently reading “PHP pdo_quote function,” an entry on BRADINO
- Published:
- 2.26.07 / 12am
- Category:
- PHP Functions
- Tags:
No comments
Jump to comment form | comments rss [?] | trackback uri [?]