Advertisement
eliax1996

php object oriented class connect db [error to implement]

Feb 22nd, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.38 KB | None | 0 0
  1. <?php
  2.  
  3. class Database {
  4.  
  5.     private $nomeTabella;
  6.     private $conn;
  7.  
  8.     public function connettiAlDB(){
  9.         $username = "prova"; $password = "gtgt";
  10.         try{
  11.                 // connessione
  12.                 $conn = new PDO("mysql:host=localhost;dbname=my_tesinandroid", $username, $password);
  13.                 // abilita gestione errori tramite try ? catch (Exception)
  14.                 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  15.             } catch(PDOException $ex){
  16.                 die("Errore connessione: ".$ex->getMessage());
  17.             }
  18.     }
  19.  
  20.     public function selezionaTabella($nomeTabella){
  21.         $this->$nomeTabella = $nomeTabella;
  22.     }
  23.  
  24.     public function scriviSulDB($sql){
  25.      
  26.       //SCRIVERE SU DB
  27.      
  28.       try {
  29.         $stmSql = $conn->prepare($sql);
  30.         //    $stmSql ->bindParam(1, $nome);
  31.         //    $stmSql ->bindParam(2, $telefono);
  32.         $risultato = $stmSql ->execute();
  33.         return $risultato;
  34.       } catch (PDOException $ex) {
  35.         echo("Query errata".$ex->getMessage());
  36.       }
  37.    
  38.     }
  39.  
  40.  
  41.   public function leggiDalDB($sql){
  42.    
  43.       //LEGGERE DA DB
  44.      
  45.       try {
  46.         $stmSql = $conn->prepare($sql);
  47.         $result = $stmSql ->execute();
  48.         return $result;
  49.       } catch (PDOException $ex) {
  50.         die("Query errata".$ex->getMessage());
  51.       }
  52.   }
  53.  
  54. }
  55.  
  56. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement