PDO::prepare

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

PDO::prepare Prepares a statement for execution and returns a statement object

Beschreibung

public PDO::prepare(string $query, array $options = []): PDOStatement|false

Prepares an SQL statement to be executed by the PDOStatement::execute() method. The statement template can contain zero or more named (:name) or question mark (?) parameter markers for which real values will be substituted when the statement is executed. Both named and question mark parameter markers cannot be used within the same statement template; only one or the other parameter style. Use these parameters to bind any user-input, do not include the user-input directly in the query.

You must include a unique parameter marker for each value you wish to pass in to the statement when you call PDOStatement::execute(). You cannot use a named parameter marker of the same name more than once in a prepared statement, unless emulation mode is on.

Hinweis:

Parameter markers can represent a complete data literal only. Neither part of literal, nor keyword, nor identifier, nor whatever arbitrary query part can be bound using parameters. For example, you cannot bind multiple values to a single parameter in the IN() clause of an SQL statement.

Calling PDO::prepare() and PDOStatement::execute() for statements that will be issued multiple times with different parameter values optimizes the performance of your application by allowing the driver to negotiate client and/or server side caching of the query plan and meta information. Also, calling PDO::prepare() and PDOStatement::execute() helps to prevent SQL injection attacks by eliminating the need to manually quote and escape the parameters.

PDO will emulate prepared statements/bound parameters for drivers that do not natively support them, and can also rewrite named or question mark style parameter markers to something more appropriate, if the driver supports one style but not the other.

Hinweis: The parser used for emulated prepared statements and for rewriting named or question mark style parameters supports the non standard backslash escapes for single- and double quotes. That means that terminating quotes immediately preceeded by a backslash are not recognized as such, which may result in wrong detection of parameters causing the prepared statement to fail when it is executed. A work-around is to not use emulated prepares for such SQL queries, and to avoid rewriting of parameters by using a parameter style which is natively supported by the driver.

As of PHP 7.4.0, question marks can be escaped by doubling them. That means that the ?? string will be translated to ? when sending the query to the database.

Parameter-Liste

query

This must be a valid SQL statement template for the target database server.

options

This array holds one or more key=>value pairs to set attribute values for the PDOStatement object that this method returns. You would most commonly use this to set the PDO::ATTR_CURSOR value to PDO::CURSOR_SCROLL to request a scrollable cursor. Some drivers have driver-specific options that may be set at prepare-time.

Rückgabewerte

If the database server successfully prepares the statement, PDO::prepare() returns a PDOStatement object. If the database server cannot successfully prepare the statement, PDO::prepare() returns false or emits PDOException (depending on error handling).

Hinweis:

Emulated prepared statements does not communicate with the database server so PDO::prepare() does not check the statement.

Beispiele

Beispiel #1 SQL statement template with named parameters

<?php
/* Execute a prepared statement by passing an array of values */
$sql 'SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :colour'
;
$sth $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute(array('calories' => 150'colour' => 'red'));
$red $sth->fetchAll();
/* Array keys can be prefixed with colons ":" too (optional) */
$sth->execute(array(':calories' => 175':colour' => 'yellow'));
$yellow $sth->fetchAll();
?>

Beispiel #2 SQL statement template with question mark parameters

<?php
/* Execute a prepared statement by passing an array of values */
$sth $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < ? AND colour = ?'
);
$sth->execute(array(150'red'));
$red $sth->fetchAll();
$sth->execute(array(175'yellow'));
$yellow $sth->fetchAll();
?>

Beispiel #3 SQL statement template with question mark escaped

<?php
/* note: this is only valid on PostgreSQL databases */
$sth $dbh->prepare('SELECT * FROM issues WHERE tag::jsonb ?? ?');
$sth->execute(['feature']);
$featureIssues $sth->fetchAll();
$sth->execute(['performance']);
$performanceIssues $sth->fetchAll();
?>

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