Visualizzazione dei risultati da 1 a 4 su 4

Discussione: Classe MySQL

  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2008
    Messaggi
    1,316

    Classe MySQL

    Codice PHP:
    class Database
    {

        var 
    $config;
        var 
    $p_connect false;
        var 
    $db;
        var 
    $num_query 0;
        
        function 
    Database($config$p_connect)
        {
            
    $this->config $config;
            
    $this->p_connect $p_connect;
            
    $this->num_query 0;

            
    $this->connect();
            
    $this->select_db();
        }

        function 
    connect()
        {
            if (
    $this->p_connect == true)
                
    $this->db mysql_pconnect($this->config['dbhost'], $this->config['dbuser'], $this->config['dbpass']);
            else
                
    $this->db mysql_connect($this->config['dbhost'], $this->config['dbuser'], $this->config['dbpass']);
            
            if (!
    $this->db)
                die(
    'Unable to connect to MySQL server. MySQL reported: '.mysql_error());

        }

        function 
    select_db()
        {
            if(!
    mysql_select_db($this->config['dbname']))
                die(
    'Unable to select database. MySQL reported: '.mysql_error());
        }

    # ................. 
    Errore: Unable to select database. MySQL reported: No database selected

  2. #2

  3. #3
    Utente di HTML.it
    Registrato dal
    Jun 2008
    Messaggi
    1,316
    Codice PHP:
    <?php

    $CONFIG
    ['dbhost'] = 'localhost';
    $CONFIG['dbuser'] = 'username';
    # ...

    $init = new Database($CONFIGfalse);

    ?>

  4. #4
    Utente di HTML.it
    Registrato dal
    Mar 2009
    Messaggi
    12

    Re: Classe MySQL

    Originariamente inviato da zacca94
    Codice PHP:
    class Database
    {

        var 
    $config;
        var 
    $p_connect false;
        var 
    $db;
        var 
    $num_query 0;
        
        function 
    Database($config$p_connect)
        {
            
    $this->config $config;
            
    $this->p_connect $p_connect;
            
    $this->num_query 0;

            
    $this->connect();
            
    $this->select_db();
        }

        function 
    connect()
        {
            if (
    $this->p_connect == true)
                
    $this->db mysql_pconnect($this->config['dbhost'], $this->config['dbuser'], $this->config['dbpass']);
            else
                
    $this->db mysql_connect($this->config['dbhost'], $this->config['dbuser'], $this->config['dbpass']);
            
            if (!
    $this->db)
                die(
    'Unable to connect to MySQL server. MySQL reported: '.mysql_error());

        }

        function 
    select_db()
        {
            if(!
    mysql_select_db($this->config['dbname']))
                die(
    'Unable to select database. MySQL reported: '.mysql_error());
        }

    # ................. 
    Errore: Unable to select database. MySQL reported: No database selected

    L'errore è piuttosto chiaro: non selezioni il database.

    Tu richiami in sequenza i seguenti metodi nel costruttore della classe:

    $this->connect();
    $this->select_db();


    ma in connect() fai subito un controllo per verificare se la connessione è o no andata a buon fine ed essendo a quel punto il database non ancora selezionato ovviamente ti riporta l'errore, quello che devi fare è eliminare dal costruttore il richiamo di questo metodo ( $this->select_db(); ) e inserirlo nel connect();

    Tipo così:

    Codice PHP:
    function Database($config$p_connect)
        {
            
    $this->config $config;
            
    $this->p_connect $p_connect;
            
    $this->num_query 0;

            
    $this->connect();

        }

        function 
    connect()
        {
            if (
    $this->p_connect == true)
                
    $this->db mysql_pconnect($this->config['dbhost'], $this->config['dbuser'], $this->config['dbpass']);
            else
                
    $this->db mysql_connect($this->config['dbhost'], $this->config['dbuser'], $this->config['dbpass']);
            
            
    self::select_db();

            if (!
    $this->db)
                die(
    'Unable to connect to MySQL server. MySQL reported: '.mysql_error());

        }

        function 
    select_db()
        {
            if(!
    mysql_select_db($this->config['dbname']))
                die(
    'Unable to select database. MySQL reported: '.mysql_error());
        } 

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.