Les API php pour MySQL
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
330
On Slideshare
329
From Embeds
1
Number of Embeds
1

Actions

Shares
Downloads
9
Comments
0
Likes
0

Embeds 1

http://www.slideee.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. PHP Mysql
  • 2. Trois Extensions (API Application Programming Interface)  Mysql  PDO  Mysqli
  • 3. Mysql  Interface Procédurale  mysql_connect($host,$login,$pass), mysql_select_db(), mysql_query()  mysql_error(), mysql_errno(), mysql_connect_error()  mysql_fetch_assoc(), mysql_fetch_object() …  Existe depuis PHP2  Obsolète depuis mysql 4.1.3 : ne prend pas en charge les nouvelles fonctionnalités mysql5 : procédure stockées, fonctions, transactions …  Absence des requêtes préparées et gestion manuelle des échappements (mysql_real_escape_strign() ou addslashes() )
  • 4. PDO (PHP Data Objects)  Interface Objet  $pdo=new PDO($dns, $login, $pass);  $pdo->query($sql); $resultat->fetch(PDO::FETCH_OBJ)  Existe depuis php 5.1  Couche d’abstraction générale avec support de plusieurs base de données.  Requêtes préparées coté client, bind des variables …
  • 5. mysqli  Le remplacement de mysql  Offre une interface à la fois procédurale et objet  Existe depuis php5  Supporte les requêtes préparées, requêtes multiples, transactions ..  Capacités de débogage avancées  Support du serveur embarqué
  • 6. mysqli Procédurale Orienté objet $cnx = mysqli_connect("example.com", "user", "password", "database"); $cnx = new mysqli("example.com", "user", "password", "database"); $result= mysqli_query($cnx,$query); $result=$cnx->query($sql); while($row= mysqli_fetch_assoc($result)) while($row=$result->fetch_assoc()) mysqli_free_result($result) $result->close(); mysqli_close($cnx); $cnx->close(); mysqli_set_charset($cnx,'utf8'); $cnx->set_charset('utf8');
  • 7. Liens utiles  http://php.net/manual/fr/mysqlinfo.api.choosing.php  http://php.net/manual/fr/mysqli.overview.php
  • 8. Récapitulatif Exemples
  • 9. //connexion avec l’extension mysql $mysql = mysql_connect("localhost", "root", ""); mysql_select_db("test"); $res = mysql_query("SELECT * FROM table", $mysql); while($row = mysql_fetch_assoc($res)){ echo $row[‘champs']; }
  • 10. //connexion avec l’extension mysqli //procédurale $mysqli = mysqli_connect("example.com", "user", "password", "database"); if (mysqli_connect_errno($mysqli)) { echo "Echec lors de la connexion à MySQL : " . mysqli_connect_error(); } $res = mysqli_query($mysqli, "SELECT * FROM table"); while($row = mysqli_fetch_assoc($res)){ echo $row[‘champs']; } //POO $mysqli = new mysqli("example.com", "user", "password", "database"); if ($mysqli->connect_errno) { echo "Echec lors de la connexion à MySQL : " . $mysqli->connect_error(); } $res = $mysqli->query("SELECT * FROM table"); while($row = $res->fetch_assoc() ){ echo $row[‘champs']; }
  • 11. //connexion avec l’extension PDO $pdo = new PDO('mysql:host=example.com;dbname=database', 'user', 'password'); $statement = $pdo->query("SELECT * FROM table"); while($row = $statement->fetch (PDO::FETCH_ASSOC)){ echo $row[‘champs']; }