Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Connessione a MySQL tramite classe

    Buon giorno,

    proprio quà in HTML.IT ho trovato nelle raccolte di script scaricabili questa classe che gestisce la connessione e relativa query a MySQL ...

    Unico problema ... non riesco a farla funzionare ... Credo sia una cosa banale ma non trovo proprio soluzione ... ad esempio se volessi stampare il contenuto di una tabella del mio db ... che codice devo scrivere???

    Codice PHP:
    ini_set('error_reporting'E_ALL);
    ini_set("display_errors"1);

    $My_db=new base_mysql("localhost""root""root""miodatabase");
            
    $My_db->base_Open();
            
    $My_db->base_Query("SELECT * FROM url");
            
              

            
    $My_db->base_Close();
            
               
    /*or
           $My_db=new base_mysql($DB_HostName, $DB_user, $DB_Password, "");
           $My_db->base_Open();
           $My_db->base_Select_DB($DB_Name);
           $My_db->base_Query($my_query);
            ... your code ...
           $My_db->base_Select_DB($OtherDB_Name);
           etc.*/

    class base_mysql {
      
    /* public variables*/


      /* private variables */
      
    var $_DB_Name;
      var 
    $_DB_User;
      var 
    $_DB_Pass;
      var 
    $_DB_Host;
      var 
    $_DB_Link;
      var 
    $_query_ID;
      var 
    $_row;
      var 
    $_debug;
      var 
    $_error_handling;
      var 
    $_stop_on_error;
        
      
    /* private constants*/
      
    var $_terminated;

    /********* CLASS CONSTRUCTOR *********/
      
    function base_mysql ($Host_DB="localhost"$User_DB="root"$Pass_DB="",$Name_DB="")
      {
        
    $this->base_Continue();
        
    $this->base_errors();
        
    $this->base_Debug();
        
    $this->_DB_Name $Name_DB;
        
    $this->_DB_User $User_DB;
        
    $this->_DB_Pass $Pass_DB;
        
    $this->_DB_Host $Host_DB;
        
    $this->_DB_link 0;
        
    $this->_query_ID 0;
        
    $this->_terminated "
    [b]!!!SCRIPT TERMINATED CAUSE ERROR!!![/b]"
    ;

      }



    /*********  SETTINGS  *********/

      
    function base_Debug($ON_OFF False)
      
    /* 
         ON_OFF = False - deactivate debug messages.
         ON_OFF = True - activate debug messages.
      */
      
    {
       
    $this->_debug=$ON_OFF;
      }
      
        function 
    base_errors($ON_OFF True)
      
    /* 
         ON_OFF = False - deactivate error handling.
         ON_OFF = True - activate error handling.
      */
      
    {
       
    $this->_error_handling=$ON_OFF;
      }
      
      function 
    base_Continue($ON_OFF True)
      
    /* 
         ON_OFF = False - stop script after an error.
         ON_OFF = True - don't stop script after an error.
      */
      
    {
       
    $this->_stop_on_error $ON_OFF;
      }
      

    /********* CLASS METHODS *********/  
      
    function base_Open()
      { 
        if (
    $this->_debug)
        { echo 
    "
    [i]Hostname: [b]
    $this->_DB_Host[/b]
    "
    ;
          echo 
    "User: [b]$this->_DB_User[/b][/i]
    "
    ;
        }
        if ((
    $this->_DB_User=="") || ($this->_DB_Host=="")) {$this->_errors(1);}
        else
        {
         
    $this->_DB_Link mysql_connect ($this->_DB_Host$this->_DB_User$this->_DB_Pass);
         if (!
    $this->_DB_Link)
          {
    $this->_errors();}
         else
          { if (
    $this->_debug==True) {echo "
    [i]Connection to DB server [b]
    $this->_DB_Host[/b] active.[/i]
    "
    ;}
            if (
    $this->_DB_Name!="") {$this->base_Select_DB($this->_DB_Name);}
          }
        } 
      }

      
      function 
    base_Close()
      {
        if (
    $this->_DB_Link)
        {
         if (
    $this->_debug==True) {echo "
    [i]Closing connection to DB server [b]
    $this->_DB_Host[/b].[/i]
    "
    ;}
         if (!
    mysql_close($this->_DB_Link)) $this->_errors();
        }
      }

     
      function 
    base_Select_DB ($other_DB_Name="")
      {
       if (
    $other_DB_Name=="") {$this->_errors(2);}
       else
       {
        if (
    $this->_DB_Link==0) {$this->_errors(4);}
        else
        {
         if (
    mysql_select_db($other_DB_Name$this->_DB_Link))
          {
    $this->_DB_Name $other_DB_Name;
           if (
    $this->_debug==True) {echo "
    [i]Database [b]
    $this->_DB_Name[/b] selected.[/i]
    "
    ;}
          }
         else {
    $this->_errors();}
        }
       }    
      }
      

      function 
    base_Get_DB()
      { 
    /* returns the current selected DB or empty if none */
       
    return $this->_DB_Name
      } 


      function 
    base_Query($_sql)
      { 
    /* execute a query */
       
    if (!$this->_DB_Link) {$this->_errors(4);}
       else
       {
        if (
    $this->_debug==True) {echo"
    [i]Executing query[/i]: [b]
    $_sql[b].
    "
    ;}
        
    $this->_query_ID mysql_query ($_sql$this->_DB_Link);
        if (!
    $this->_query_ID)
         {
    $this->_errors();
          unset (
    $_sql);
          return 
    False;}
        else
        {unset (
    $_sql);
         return 
    True;}
       }
      }
     
      
      function 
    base_Num_Rows()
      {
       if (
    $this->_query_ID!=0)
        { 
         
    $Number mysql_num_rows($this->_query_ID);
         if (
    is_null($Number)){return "NULL";}
         else {return 
    $Number;}
        }
       else 
        {
         
    $this->_errors(3);
         return 
    False;
        }
      }
     
      function 
    base_Affected_Rows()
      {
       if (
    $this->_DB_Link!=0)
        {
         
    $Number mysql_affected_rows($this->_DB_Link);
         if (
    is_null($Number)){return "NULL";}
         else {return 
    $Number;}
        }
       else 
        {
         
    $this->_errors(4);
         return 
    False;
        }
      } 
      
      function 
    base_Data_Seek($row_number=0)
      {
       if (
    $this->_query_ID!=0)
        { 
    mysql_data_seek($this->_query_ID,$row_number);}
       else 
        {
         
    $this->_errors(3);
         return 
    False;
        } 
      }
      
      
      function 
    base_Fetch_Row()
      {
       if (
    $this->_query_ID!=0)
        { return 
    mysql_fetch_row($this->_query_ID);}
       else 
        {
         
    $this->_errors(3);
         return 
    False;
        }    
      }
      
      

     
     
    /********* ERROR HANDLING FUNCTION *********/ 
      
    function _errors ($int_err=0)
      {if (!
    $this->_error_handling) {return False;}
       else
       {
        switch (
    $int_err)
        {
        case 
    0/* for My_SQL errors handling */
          
    echo "
    [b]Error number:[/b] 
    $mysql_errno()
    "
    ;
          echo 
    "[b]Error Text:[/b] $mysql_error()
    "
    ;
          break;
          
        case 
    1:
          echo 
    "
    [b]Please specify User/Host![/b]
    "
    ;
          break;
        
        case 
    2:
          echo 
    "
    [b]Database name not set.[/b]
    "
    ;
          break; 
          
        case 
    3:
          echo 
    "
    [b]No valid query have been executed.[/b]
    "
    ;
          break;
        
        case 
    4:
          echo 
    "
    [b]No DB connection available.[/b]
    "
    ;
          break;

        default:
          break;
        } 
        if (!
    $this->_stop_on_error) die($this->_terminated);
       }
      }
      
     
    /********* OTHER FUNCTIONS *********/
     
    function _is_boolean($_to_check)
     {
    /* for future use */
      
    if (is_bool($_to_check))
       {return 
    True;}
      else
       {
    $this->_errors(4);
        return 
    False;}
     }
      


  2. #2
    Utente di HTML.it
    Registrato dal
    Apr 2004
    Messaggi
    3,709
    probabilmente c'è un problema di connessione e query...

    in questa riga:
    codice:
    $My_db=new base_mysql("localhost", "root", "root", "miodatabase");
    devi mettere il server corretto (probabilmente "localhost" va bene, ma non è detto), il nome utente e la password (magari non sono "root" e "root"?) ed il nome del database (è "miodatabase"?), mentre in quest'altra:
    codice:
    $My_db->base_Query("SELECT * FROM url");
    la query desiderata (nell'esempio si fa una selezione di tutti i records della tabella "url", ma deve esistere!)

    Se tutto è ok poi bisogna fare il fetching se ci sono dati da recuperare, qualcosa come:
    Codice PHP:
    while ($data $My_db->base_Fetch_Row()) {
      
    print_r($data);
    }; 
    e questo prima del finale $My_db->base_Close();

  3. #3
    Originariamente inviato da eiyen
    probabilmente c'è un problema di connessione e query...

    in questa riga:
    codice:
    $My_db=new base_mysql("localhost", "root", "root", "miodatabase");
    devi mettere il server corretto (probabilmente "localhost" va bene, ma non è detto), il nome utente e la password (magari non sono "root" e "root"?) ed il nome del database (è "miodatabase"?), mentre in quest'altra:
    codice:
    $My_db->base_Query("SELECT * FROM url");
    la query desiderata (nell'esempio si fa una selezione di tutti i records della tabella "url", ma deve esistere!)

    Se tutto è ok poi bisogna fare il fetching se ci sono dati da recuperare, qualcosa come:
    Codice PHP:
    while ($data $My_db->base_Fetch_Row()) {
      
    print_r($data);
    }; 
    e questo prima del finale $My_db->base_Close();
    Perfetto!!!!!! $data è un Array ... quindi basta inserire $data[0], $data[1] o $data[3] a seconda del ampo che vuoi ottenere per avere il relativo dato

    Ma se volessi ottenere ad esempio i valori della colonna url???? Come posso fare????

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 © 2024 vBulletin Solutions, Inc. All rights reserved.