Posted By

alexandrepayet on 04/09/10


Tagged

database php object base utf8 mysqli connect result close donne connexion objet syntaxe


Versions (?)

PHP : Utilisation de mysqli (syntaxe objet)


 / Published in: PHP
 

  1. <?php
  2.  
  3. /* how to create connexion (4th parameter = default database) */
  4. $mysqli = new mysqli($host, $usr, $pass, $db);
  5.  
  6. /* how to verify connexion */
  7. if (mysqli_connect_errno()) {
  8. printf("Echec de la connexion : %s\n", mysqli_connect_error());
  9. }
  10.  
  11. /* how to set the default charset to use */
  12. if (!$mysqli->set_charset("utf8")) {
  13. printf("Erreur lors du chargement du jeu de caractères utf8 : %s\n", $mysqli->error);
  14. }
  15.  
  16. /* how to use real_escape_string on string */
  17. $nom = $mysqli->real_escape_string($nom);
  18. $mail = $mysqli->real_escape_string($mail);
  19. $tel = $mysqli->real_escape_string($tel);
  20.  
  21.  
  22. /*how to get data in a table with a while */
  23. $query = "SELECT nom, mail, tel FROM ufr_jury ORDER by nom;";
  24. $ressource = $mysqli->query($query);
  25. while($result = $ressource->fetch_assoc()){
  26.  
  27. echo "<tr>";
  28.  
  29. echo "<td>".$result['nom']."</td>";
  30. echo "<td>".$result['mail']."</td>";
  31. echo "<td>".$result['tel']."</td>";
  32.  
  33. echo "</tr>";
  34.  
  35. }
  36.  
  37. /* how to free result ressource */
  38. $ressource->close();
  39.  
  40. /* how to close connection */
  41. $mysqli->close();
  42.  
  43. ?>

Report this snippet  

You need to login to post a comment.