PHP : Utilisation de mysqli (syntaxe objet)


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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. printf("Echec de la connexion : %s\n", mysqli_connect_error());
  8. }
  9.  
  10. /* how to set the default charset to use */
  11. if (!$mysqli->set_charset("utf8")) {
  12. printf("Erreur lors du chargement du jeu de caractères utf8 : %s\n", $mysqli->error);
  13. }
  14.  
  15. /* how to use real_escape_string on string */
  16. $nom = $mysqli->real_escape_string($nom);
  17. $mail = $mysqli->real_escape_string($mail);
  18. $tel = $mysqli->real_escape_string($tel);
  19.  
  20.  
  21. /*how to get data in a table with a while */
  22. $query = "SELECT nom, mail, tel FROM ufr_jury ORDER by nom;";
  23. $ressource = $mysqli->query($query);
  24. while($result = $ressource->fetch_assoc()){
  25.  
  26. echo "<tr>";
  27.  
  28. echo "<td>".$result['nom']."</td>";
  29. echo "<td>".$result['mail']."</td>";
  30. echo "<td>".$result['tel']."</td>";
  31.  
  32. echo "</tr>";
  33.  
  34. }
  35.  
  36. /* how to free result ressource */
  37. $ressource->close();
  38.  
  39. /* how to close connection */
  40. $mysqli->close();
  41.  
  42. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.