Baoulettes

[php]Old to new

Sep 11th, 2019
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.33 KB | None | 0 0
  1. <?php
  2.     /*from mysqli to PDO
  3.     Avant en MSQLI fallait faire ca pour la connection :
  4. ------------------------------------------------------------------ 
  5.     $BD_serveur     = "localhost";
  6.     $BD_utilisateur = "root";
  7.     $BD_motDePasse  = "";
  8.     $BD_base        = "animehun_site";
  9.     $url_base       = "/oioiji/v1/";
  10.     $mysqli = new mysqli($BD_serveur,$BD_utilisateur, $BD_motDePasse,$BD_base);
  11.     if ($mysqli->connect_errno) {
  12.         printf("Échec de la connexion : %s\n", $mysqli->connect_error);  
  13.         exit();  
  14.     }
  15.     $mysqli->set_charset("utf8");
  16. ------------------------------------------------------------------
  17.  
  18.     Maintenant en PDO ca ce passe comme cela :
  19. ------------------------------------------------------------------
  20.     $errors     =   array();
  21.     $RemoteIP   =   $_SERVER['REMOTE_ADDR'];
  22.     $options    =   array(
  23.         PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  24.     );
  25.     try {
  26.         $pdo = new PDO('mysql:host=localhost;dbname=Nom_Database', 'COMPTE_SQL', 'PASSWORD',$options); //Test de ce co Si c'est en local !
  27.     }
  28.     catch(Exception $e){}
  29.     finally { //si il a pas réussi a ce co en local (donc il est sur un dédiers)
  30.         if (isset($e)){
  31.             try {
  32.                 $pdo = new PDO('mysql:host=localhost;dbname=Nom_Database', 'COMPTE_SQL', 'PASSWORD',$options); //Remote !
  33.             }
  34.             catch(Exception $e2){
  35.                 //print_r($e); // a decomment si tu veux les erreures
  36.             }
  37.             finally {
  38.                 if (isset($e2)){
  39.                     //de la tu peux faire un echo ou print des erreures ;)
  40.                     exit;
  41.                 }
  42.             }
  43.         }
  44.     }
  45. ------------------------------------------------------------------
  46.  
  47. Maintenant en utilisation MSSQLI tu fait ecla :
  48. ------------------------------------------------------------------
  49.     $sql    =   "   SELECT * FROM news WHERE date > '".$daten."' AND date <= '".$dateplus."'ORDER by date DESC";
  50.     if(!$result = $mysqli->query($sql)){
  51.         die('There was an error running the query [' . $db->error . ']');
  52.     }
  53.     $datebdd1   =   '';
  54.     while($row  =   $result->fetch_assoc()){
  55.         $   = substr($row['date'],0,-9);
  56.         $titreEpi   = $row['titre'];
  57.         $episode    = $row['episode'];
  58.         $numeroEpi  = $row['numero'];
  59.         $version    = $row['version'];
  60.         $saison     = $row['saison'];
  61.         $urlimg     = $row['urlimg'];
  62.         $url        = $row['url']; //pas besoin de deux fois la variable ici au passage
  63.         $url        = str_replace('/anime/',$url_base.'visio/',$url);
  64.         $newser     = $row['ajouteur'];
  65.         $render     = $row['render'];
  66.         $text       = $row['text'];
  67.         $comment    = $row['comment'];
  68.         $etat       = $row['etat'];
  69.     }
  70. ------------------------------------------------------------------
  71.  
  72. Sous PDO ca se passera comme ca :
  73.  
  74. ------------------------------------------------------------------
  75.     $CommandName = $pdo->prepare("SELECT * FROM news WHERE date > '$daten' AND date <= '$dateplus' ORDER by date DESC");
  76.     $CommandName->execute();
  77.     if ($CommandName->rowCount() > 0) {
  78.         while ($row = $CommandName->fetch(PDO::FETCH_ASSOC)){
  79.             $datebdd    = substr($row['date'],0,-9);
  80.             $titreEpi   = $row['titre'];
  81.             $episode    = $row['episode'];
  82.             $numeroEpi  = $row['numero'];
  83.             $version    = $row['version'];
  84.             $saison     = $row['saison'];
  85.             $urlimg     = $row['urlimg'];
  86.             $url        = str_replace('/anime/',$url_base.'visio/',$row['url']);
  87.             $newser     = $row['ajouteur'];
  88.             $render     = $row['render'];
  89.             $text       = $row['text'];
  90.             $comment    = $row['comment'];
  91.             $etat       = $row['etat'];
  92.         }
  93.     }
  94. ------------------------------------------------------------------
  95. */
  96. ?>
Advertisement
Add Comment
Please, Sign In to add comment