PHP-Einfach.de
  • PHP Tutorial
  • MySQL Tutorial
  • Für Fortgeschrittene
  • Webhosting
  • Forum

Crashkurs PDO

15. Juli 2019
  1. Home
  2. »
  3. MySQL Tutorial
  4. »
  5. Crashkurs PDO

Eine ausführliche Einführung in MySQL und PHP Data Objects (PDO) findet ihr in unserem MySQL Tutorial. Dies ist nur eine kurze Übersicht der wichtigen Funktionen von PDO.

 

Inhaltsverzeichnis

  • 1 Verbindung aufbauen
  • 2 SQL Query an Datenbank senden
  • 3 Prepared Statements
  • 4 Anzahl der Zeilen
  • 5 Eingefügte ID
  • 6 MySQL Fehlermeldung

Verbindung aufbauen

Verbindung zur MySQL-Datenbank mittels PDO:

1
2
3
<?php
$pdo = new PDO('mysql:host=localhost;dbname=databasename', 'username', 'password');
?>

Ausführliche Informationen

SQL Query an Datenbank senden

Simple Datenabfragen können mittels $pdo->query($sql) an die Datenbank gesendet werden.

1
2
3
4
5
6
7
8
9
10
<?php
$pdo = new PDO('mysql:host=localhost;dbname=databasename', 'username', 'password');
$sql = "SELECT * FROM users";
foreach ($pdo->query($sql) as $row) {
   echo $row['email']."<br />";
   echo $row['vorname']."<br />";
   echo $row['nachname']."<br /><br />";
}
?>

Ausführliche Informationen

Prepared Statements

Prepared Statement mit anonymen Parametern:

1
2
3
4
5
6
7
8
9
10
<?php
$pdo = new PDO('mysql:host=localhost;dbname=databasename', 'username', 'password');
$statement = $pdo->prepare("SELECT * FROM users WHERE vorname = ? AND nachname = ?");
$statement->execute(array('Max', 'Mustermann'));  
while($row = $statement->fetch()) {
   echo $row['vorname']." ".$row['nachname']."<br />";
   echo "E-Mail: ".$row['email']."<br /><br />";
}
?>

Prepared Statement mit benannten Parametern:

1
2
3
4
5
6
7
8
9
10
<?php
$pdo = new PDO('mysql:host=localhost;dbname=databasename', 'username', 'password');
$statement = $pdo->prepare("SELECT * FROM users WHERE vorname = :vorname AND nachname = :nachname");
$statement->execute(array(':vorname' => 'Max', ':nachname' => 'Mustermann'));  
while($row = $statement->fetch()) {
   echo $row['vorname']." ".$row['nachname']."<br />";
   echo "E-Mail: ".$row['email']."<br /><br />";
}
?>

Ausführliche Informationen

Anzahl der Zeilen

Die Anzahl der betroffenen Zeilen können mittels der Methode rowCount() ermittelt werden:

1
2
3
4
5
6
7
8
<?php
$pdo = new PDO('mysql:host=localhost;dbname=databasename', 'username', 'password');
  
$statement = $pdo->prepare("SELECT * FROM users WHERE vorname = ?");
$statement->execute(array('Max'));
$anzahl_user = $statement->rowCount();
echo "Es wurden $anzahl_user gefunden";
?>

Ausführliche Informationen

Eingefügte ID

Die vergebene ID einer Auto Increment-Spalte kann mittels der Methode lastInsertId() abgerufen werden.

1
2
3
4
5
6
7
8
9
<?php
$pdo = new PDO('mysql:host=localhost;dbname=databasename', 'username', 'password');
$statement = $pdo->prepare("INSERT INTO users (email, vorname, nachname) VALUES (?, ?, ?)");
$statement->execute(array('[email protected]', 'Klaus', 'Neumann'));  
$neue_id = $pdo->lastInsertId();
echo "Neuer Nutzer mit id $neue_id angelegt";
?>

Ausführliche Informationen

MySQL Fehlermeldung

Zum Abrufen der MySQL Fehlermeldung existiert die Methode errorInfo().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
$statement = $pdo->prepare("SELECT email, password FROM users");
if($statement->execute()) {
    while($row = $statement->fetch()) {
        echo $row['email']."<br />";
    }    
} else {
    echo "SQL Error <br />";
    echo $statement->queryString."<br />";
    echo $statement->errorInfo()[2];
}
?>

Ausführliche Informationen

Autor: Nils Reimers
Zurück: Crashkurs MySQLi

MySQL Tutorial

  • MySQL installieren und starten
  • phpMyAdmin
  • Verbindung aufbauen
  • Simple Datenabfrage
  • Komplexere Datenabfrage
  • Prepared Statements
  • Daten einfügen
  • Daten aktualisieren
  • Daten löschen
  • Gefundene Einträge zählen
  • DATE-Datenbankspalten
  • Datum- und Zeitfunktionen
  • MySQL Fehlermeldungen
  • Datenbankverbindung schließen
  • JOIN
  • Indizes
  • Normalformen
  • Fremdschlüssel
  • Übersicht SQL-Befehle
  • Crashkurs MySQL
  • Crashkurs MySQLi
  • Crashkurs PDO
Mit freundlicher Unterstützung von:
  • Punkt191 Werbeagentur
  • DGUV V3
  • DGUV 3
  • CasinoAndy Finland
  • Casinopilot24.com
  • Neueonline-Casinos.com
  • CasinoHEX.at
  • Decasinos.de
  • Privatkredit247.com
  • Online Casino Spielautomaten
  • Casinofrog.com
  • parhaatuudetkasinot.com

Hoster – Geringste Ausfallzeit

  1. webgo Ø 0 Min.
  2. Linevast Ø 3 Min.
  3. netcup Ø 3 Min.
  4. dogado Ø 6 Min.
  5. Mittwald Ø 9 Min.
  6. All-Inkl.com Ø 10 Min.
  7. manitu Ø 10 Min.
  8. bplaced Ø 11 Min.
  9. checkdomain Ø 11 Min.
  10. Host Europe Ø 14 Min.
» Mehr erfahren

Impressum | Datenschutz | Auf PHP-Einfach.de werben

© PHP-Einfach.de 2003 - 2022

Um dich beim Lernen von PHP und MySQL zu unterstützen verwenden wir Cookies. OK Weitere Infos
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Notwendige
immer aktiv

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Nicht notwendige

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.