Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2008
    Messaggi
    126

    utilizzo della classe database per tutte le classi

    Salve a tutti!
    Di seguito ho questa classe che mi permette la connessione al db e di effettuare query...chiudere connessione...ecc.
    Leggendo di seguito al codice postato vi pongo il problema.
    Codice PHP:
    class DBData
    {
      protected 
    $name      "name"
      protected 
    $hostname  "localhost";
      protected 
    $username  "xxx";
      protected 
    $password  "xxx";
      protected 
    $linkid    NULL;
      protected 
    $encoding  NULL;

        public function 
    __construct()
        {
            
    // operazioni di inizializzazione
        
    }
       
        public function 
    __destruct()
        {
            
    // operazioni eseguite prima della distruzione
        
    }
      function 
    openConnection()
      {
        
    $id = @mysql_connect($this->hostname,$this->username,$this->password);
        if (
    $id)
        {
          
    $this->linkid $id;
          
    $status mysql_select_db($this->name);
        } else {
          return 
    ERROR_CONNECTION_FAILED;
        }

        if (!
    $status)
        {
          return 
    ERROR_DB_SELECTION_FAILED;
        } else {
          return 
    FALSE;
        }
      }
    //query sulla tabella 
     
    function Query($sql){
      
    $sql = @mysql_query($sql) or die (mysql_error());
      return 
    $sql;
      }

    //estrazione di un record
     
    function FetchRow($sql){
      
    $rows = @mysql_fetch_row($sql);
      return 
    $rows;
      }

    //conteggio dei records
     
    function FetchNum($sql){
      
    $num = @mysql_num_rows($sql);
      return 
    $num;
      }

    //estrazione dei records  
      
    function FetchArray($sql){
      
    $array = @mysql_fetch_array($sql);
      return 
    $array;
      }

    //chiusura della connessione
     
    function Close(){
      @
    mysql_close();
      }
      
      function 
    closeConnection()
      {
        if (
    $this->linkid$status = @mysql_close($this->linkid);
        if (!
    $status)
        {
          return 
    ERROR_DB_CLOSED_FAILED;
        } else {
          return 
    FALSE;
        }
      }

         


    La classe funziona perfettamente.
    Il problema sta nel riutilizzo della classe nelle altre classi....
    Es.:
    Codice PHP:
    class listCity extends city
    {
        var 
    $city;
        var 
    $randCity;

        public function 
    listCity($rand,$idp)
        {

            
    $dataBase=new DBData();
            
    //apro la connessione al db
            
    $dataBase->openConnection();
            if (
    $rand!="yes" and isset($idp)) {
                
    $sql="select id,nome,descrizione,desinglese from località WHERE id=".$idp."";
                
            }
        if (
    $rand=="yes" and $idp=="") {
                
    $sql="select id,nome,descrizione,desinglese from località";
            }
            if (
    $rand=="no" and $idp=="") {
                
    $sql="select id,nome,descrizione,desinglese from località";
            }
            
    $res=$dataBase->Query($sql);

            while (
    $data=@mysql_fetch_array($res)) {
            
    $city = new city();
                
    $city->setId($data["id"]);
                
    $city->setName($data["nome"]);
                
    $city->setDescriptionIt($data["descrizione"]);
                
    $city->setDescriptionEn($data["desinglese"]);
                
            
    $cities[] = $city;

            }

                

            
    //chiudo la connessione
            
    $dataBase->closeConnection();

            if (
    $rand=="yes") {
                
    $totalCity count($cities);
                
    $theCity=rand(0,$totalCity-1);
                
    $randCity[]=$cities[$theCity];
                return 
    $randCity;
            }else { return 
    $cities;}
        }


    Oltre a quello che fa la classe.....
    Potete notare che istanzio la connessione:
    Codice PHP:
    $dataBase=new DBData();
            
    //apro la connessione al db
            
    $dataBase->openConnection(); 
    e poi la chiudo alla fine:
    Codice PHP:
            //chiudo la connessione
            
    $dataBase->closeConnection(); 
    Il problema è ..... che per usufruire della connessione al db devo sempre connettermi in ogni classe...
    Mentre io vorrei aprire una connessione in testa alla pagina web php e chiuderla alla fine, per non rallentare il sito con N connessioni al database...

    Spero di essere stato chiaro...
    Grazie

  2. #2
    Utilizza il pattern Singleton oppure passa la classe di accesso al database come parametro del costruttore delle classi che devono utilizzarla (o soluzione equivalente).

    http://www.php.net/manual/en/languag...erns.singleton

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.