PDO::quote

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

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

Beschreibung

public PDO::quote(string $string, int $type = PDO::PARAM_STR): string|false

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.

Achtung

Security: the default character set

The character set must be set either on the server level, or within the database connection itself (depending on the driver) for it to affect PDO::quote(). See the driver-specific documentation for more information.

Parameter-Liste

string

The string to be quoted.

type

Provides a hint to the type of data for drivers that have alternate quoting styles. For example PDO_PARAM_LOB will tell the driver to escape binary data.

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

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Grundlagen von Views in MySQL

Views in einer MySQL-Datenbank bieten die Möglichkeit, eine virtuelle Tabelle basierend auf dem Ergebnis einer SQL-Abfrage zu erstellen. ...

admin

Autor : admin
Kategorie: mySQL-Tutorials

Definition von Stored Procedures - eine Einführung

Stored Procedures sind vordefinierte SQL-Codeblöcke, die in einer Datenbank gespeichert sind und bei Bedarf aufgerufen werden können. ...

Bernie

Autor : ebiz-consult GmbH & Co. KG
Kategorie: mySQL-Tutorials

Wie kann man komplexe Abfragen mit SQL-Querys In MySQLi effektiv durchführen?

In diesem MySQL-Tutorial wird erklärt, wie komplexe SQL-Abfragen in MySQLi effizient durchgeführt werden können. Wir werden uns mit verschiedenen Aspekten der Datenbankabfrage beschäftigen und spezifische Methoden kennenlernen. ...

TheMax

Autor : TheMax
Kategorie: mySQL-Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

Professioneller Webentwickler & Webdesigner

Of course, here is the translation: Hello, Thank you for your interest in the long-term project. Your extensive skills and experience in web dev ...

Geschrieben von Athelstan am 15.04.2024 09:25:39
Forum: Jobgesuche
Wir stellen unsere SEO-Agentur vor

Hallo In der heutigen digitalen Welt war es für Unternehmen noch nie so einfach, ihre Reichweite weltweit zu vergrößern. Wenn Sie außerhalb I ...

Geschrieben von thomasmuller am 14.04.2024 07:18:33
Forum: User stellen sich vor
Spielplan für 4 Gruppen zu je 6 Teams auf 2 Feldern

Hallöchen zusammen, ich versuche derzeit unseren Excel-Spielplan in PHP zu überführen. Eigentlich bin ich auch shon fertig - wenn da nicht dies ...

Geschrieben von derH0st am 11.04.2024 15:58:37
Forum: PHP Developer Forum
PHP 8.3.3 - App kann auf dem PC nicht ausgeführt werden

Problem gelöst. Die php.exe hatte einen defekt. Neue heruntergeladen und fertig.

Geschrieben von Tetra1081 am 10.04.2024 16:49:14
Forum: PHP Developer Forum