CM3-2 : PDO Mickaël Martin Nevotmickael-martin-nevot.com/institut-g4/php/s11-cm3-2-pdo.pdf ·...

8
Mickaël Martin Nevot 03/04/2016 11:26 Cette œuvre de Mickaël Martin Nevot est mise à disposition selon les termes de la licence Creative Commons Attribution - Pas d'Utilisation Commerciale - Partage à l'Identique 3.0 non transposé. Cette œuvre de Mickaël Martin Nevot est mise à disposition selon les termes de la licence Creative Commons Attribution - Pas d'Utilisation Commerciale - Partage à l'Identique 3.0 non transposé. V1.0.0 CM3-2 : PDO

Transcript of CM3-2 : PDO Mickaël Martin Nevotmickael-martin-nevot.com/institut-g4/php/s11-cm3-2-pdo.pdf ·...

Page 1: CM3-2 : PDO Mickaël Martin Nevotmickael-martin-nevot.com/institut-g4/php/s11-cm3-2-pdo.pdf · 2016-05-28 · Présentation > PHP I > XML > Regexp > PHP II > MySQL > POO > PDO > Hacking

Mickaël Martin Nevot

03/04/2016 11:26

Cette œuvre de Mickaël Martin Nevot est mise à disposition selon les termes de la

licence Creative Commons Attribution - Pas d'Utilisation Commerciale - Partage à l'Identique

3.0 non transposé.

Cette œuvre de Mickaël Martin Nevot est mise à disposition selon les termes de la

licence Creative Commons Attribution - Pas d'Utilisation Commerciale - Partage à l'Identique

3.0 non transposé.

V1.0.0

CM3-2 : PDO

Page 2: CM3-2 : PDO Mickaël Martin Nevotmickael-martin-nevot.com/institut-g4/php/s11-cm3-2-pdo.pdf · 2016-05-28 · Présentation > PHP I > XML > Regexp > PHP II > MySQL > POO > PDO > Hacking

Présentation > PHP I > XML > Regexp > PHP II > MySQL > POO > PDO > Hacking > PHP « avancé » V1.0.0

I. Présentation

II. PHP I

III. XML

IV. Regexp

V. PHP II

VI. MySQL

VII. POO

VIII. PDO

IX. Hacking

X. PHP « avancé »

PHP Mickaël Martin Nevot 1/7

Page 3: CM3-2 : PDO Mickaël Martin Nevotmickael-martin-nevot.com/institut-g4/php/s11-cm3-2-pdo.pdf · 2016-05-28 · Présentation > PHP I > XML > Regexp > PHP II > MySQL > POO > PDO > Hacking

Présentation > PHP I > XML > Regexp > PHP II > MySQL > POO > PDO > Hacking > PHP « avancé » V1.0.0

PDO : PHP Data Object

Couche d’abstraction orientée objet permettant d’accéder à

une base de données

Compatible PHP 5.1+

Lisibilité des requêtes préparées accrue

SGBD supportés :

MySQL, PostGreSQL, Oracle, SQLite, ODBC, DB2, etc.

PHP Data Object

PHP Mickaël Martin Nevot

Important : l’activation de l’extension PDO est obligatoire

2/7

Page 4: CM3-2 : PDO Mickaël Martin Nevotmickael-martin-nevot.com/institut-g4/php/s11-cm3-2-pdo.pdf · 2016-05-28 · Présentation > PHP I > XML > Regexp > PHP II > MySQL > POO > PDO > Hacking

Présentation > PHP I > XML > Regexp > PHP II > MySQL > POO > PDO > Hacking > PHP « avancé » V1.0.0

MySQLi PDO

mysqli_select_db();

mysqli_num_rows(); mysqli_affected_rows();

mysqli_fetch_array(); mysqli_fetch_assoc(); mysqli_fetch_row();

mysqli_free_result();

mysqli_insert_id();

Base de données indiquée dans

le DSN

PDOStatement->rowCount()

PDOStatement->fetch()

(en indiquant le mode

d’analyse souhaité)

unset()

PDO::lastInsertId

MySQLi Vs PDO

PHP Mickaël Martin Nevot

Les performances de MySQLi sont légèrement meilleures

3/7

Page 5: CM3-2 : PDO Mickaël Martin Nevotmickael-martin-nevot.com/institut-g4/php/s11-cm3-2-pdo.pdf · 2016-05-28 · Présentation > PHP I > XML > Regexp > PHP II > MySQL > POO > PDO > Hacking

Présentation > PHP I > XML > Regexp > PHP II > MySQL > POO > PDO > Hacking > PHP « avancé » V1.0.0

try { // Connexion à la base de données.

$dsn = 'mysql:host=localhost;dbname=my_dbname'; $pdo = new PDO($dsn, 'mysql_username', 'mysql_password'); // Codage de caractères. $pdo->exec('SET CHARACTER SET utf8'); // Gestion des erreurs sous forme d'exceptions. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }

catch(PDOException $e)

{ // Affichage de l'erreur. die('Erreur : ' . $e->getMessage()); }

Exemple PDO et MySQL

PHP Mickaël Martin Nevot

DSN (data source name)

4/7

Page 6: CM3-2 : PDO Mickaël Martin Nevotmickael-martin-nevot.com/institut-g4/php/s11-cm3-2-pdo.pdf · 2016-05-28 · Présentation > PHP I > XML > Regexp > PHP II > MySQL > POO > PDO > Hacking

Présentation > PHP I > XML > Regexp > PHP II > MySQL > POO > PDO > Hacking > PHP « avancé » V1.0.0

$sql = 'SELECT champ AS username FROM table WHERE id = :id'; $stmt = $pdo->prepare($sql); // Préparation d'une requête. $id = 1; $stmt->bindValue('id', $id, PDO::PARAM_INT); // Lie les paramètres de manière sécurisée. try

{ $stmt->execute(); // Exécution de la requête. $stmt->rowCount() or die('Pas de résultat' . PHP_EOL); // S'il y a des résultats.

$stmt->setFetchMode(PDO::FETCH_OBJ); while ($result = $stmt->fetch()) { echo $result->username; // Affichage des résultats. } }

catch (PDOException $e)

{ // Affichage de l'erreur et rappel de la requête. echo 'Erreur : ', $e->getMessage(), PHP_EOL; echo 'Requête : ', $sql, PHP_EOL; exit(); }

Exemple PDO et MySQL

PHP Mickaël Martin Nevot 5/7

Page 7: CM3-2 : PDO Mickaël Martin Nevotmickael-martin-nevot.com/institut-g4/php/s11-cm3-2-pdo.pdf · 2016-05-28 · Présentation > PHP I > XML > Regexp > PHP II > MySQL > POO > PDO > Hacking

Présentation > PHP I > XML > Regexp > PHP II > MySQL > POO > PDO > Hacking > PHP « avancé » V1.0.0

try

{

// Connexion à la base de données. $dsn = 'mysql:host=localhost;dbname=my_dbname';

$dbh = new PDO($dsn, 'username', 'pwd', array(PDO::ATTR_PERSISTENT => true)); $pdo->exec('SET CHARACTER SET utf8');

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch (Exception $e) { die('Connexion impossible : ' . $e->getMessage());}

try

{

$db->beginTransaction(); // Début de transaction.

// Exécution des requêtes.

// …

$db->commit();

{

catch(Exception $e) {

$db->rollBack(); // Annulation et remise à l’état initial en cas d’erreur.

echo 'Transaction echouée : ' . $e->getMessage();

}

PDO et transactions

PHP Mickaël Martin Nevot 6/7

Page 8: CM3-2 : PDO Mickaël Martin Nevotmickael-martin-nevot.com/institut-g4/php/s11-cm3-2-pdo.pdf · 2016-05-28 · Présentation > PHP I > XML > Regexp > PHP II > MySQL > POO > PDO > Hacking

Présentation > PHP I > XML > Regexp > PHP II > MySQL > POO > PDO > Hacking > PHP « avancé » V1.0.0

Auteur

Mickaël Martin Nevot

[email protected]

Carte de visite électronique

Cours en ligne sur : www.mickael-martin-nevot.com

Relecteur

Mickaël Martin Nevot

[email protected]

Carte de visite électronique

Christophe Delagarde

([email protected])

Pierre-Alexis de Solminihac ([email protected])

Crédits

PHP Mickaël Martin Nevot

7/7