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

    NOn connessione al database

    Salve ragazze, sto cercando di imparare a programmare OOP in php, per ora è andato tutto a buon fine, fino alla connessione del db, ho creato un phpadmin un database chiamato corso, una tabella e poi ho fatto due pagina php, una contenente le classi e un altro una select, ma nonostante i dati del db siano esatti mi dice:

    codice:
    No database selectedNo database selected
    Il tutto avviene in localhost, il file delle classi è questo_

    codice:
    Prova (contiene le classi
    <?php
    codice:
    class database {
    
    
        private $hostname;
        private $username;
        private $password;
        private $database;
        private $connection;
        
        private $query;
        private $where;
        private $order;
        private $result = array();
    
    
        var $lastQuery;
    
    
        # the constructor initializes the mysql connection
        public function mysql() {
        
            $this->hostname = 'localhost';
            $this->username = 'root';
            $this->password = '';
            $this->database = 'corso';
            
            $connection = mysql_connect($this->hostname, $this->username, $this->password);
            mysql_select_db($this->database, $connection) or die(mysql_error());
            
            return true;
        
        }
    
    
        # selects the data from the table
        public function select($query, $where, $order) {
            
            $this->query = $query;
            $this->where = ($where == NULL) ? NULL : $where;
            $this->order = ($order == NULL) ? NULL : $order;
            
            $fullQuery = $this->query . $this->where . $this->order;
            $this->lastQuery = $fullQuery;
            $this->result = mysql_query($fullQuery);
            
            if ($this->result()) { return true; }
            
            return false;
            
        }
        
        # used to validate result from select()
        private function result() {
            
            if ($this->result) { return true; }
            
            echo mysql_error();
            return false;
        
        }
    
    
        # displays the data from the select() member
        public function display() {
            
            if ($this->select($this->query, $this->where, $this->order)) {
            
                echo '<h3>' . $this->lastQuery . '</h3><br />';
            
                while ($row = mysql_fetch_array($this->result, MYSQL_ASSOC)) {
                    echo '<b>' . $row['id'] . ' </b>' . $row['file_name'] . '<br />';
                }
                
                return true;
                
            }
            
            return false;
        
        }
    
    
    }
    
    
    ?>
    Secondo file

    Codice PHP:
    <?phpinclude 'prova.php';
    $photos = new database();
    $query "SELECT * FROM page_text";$where NULL;$order NULL;
    $photos->select($query$where$order);$photos->display();?>

    ma non si connette ad database in localhost
    Ringrazio chiunque mi può aiutare in anticipo

  2. #2
    Quote Originariamente inviata da Werwolfe Visualizza il messaggio
    Salve ragazze
    Non ho capito se la domanda è rivolta solo alle ragazze del forum...
    Io non lo sono... ma spero che accetterai comunque la mia risposta...

    Non si collega al database perché da nessuna parte dici di farlo:

    Quote Originariamente inviata da Werwolfe Visualizza il messaggio
    Codice PHP:
    <?phpinclude 'prova.php';
    $photos = new database();
    $query "SELECT * FROM page_text";$where NULL;$order NULL;
    $photos->select($query$where$order);$photos->display();?>
    In questo pezzo di codice crei un oggetto della classe "database", prepari la stringa SQL, poi effettui l'interrogazione... ma da nessuna parte richiami la funzione mysql() che hai dichiarato la quale fa la connessione al database...

  3. #3
    Scusa non mi rivolgo alle ragazze, scusa ieri l'ho scritto alle 4 di notte, perchè lavorando posso studiare solo la sera forse ero stanco vero quindi aggiungo al codice mysql(); vediamo se funge, grazie tante

  4. #4
    Grazie per l'aiutone, una sola domanda adesso, (funziona tutto)

    se io volessi usare questa funzione ovunque

    codice:
    # displays the data from the select() member
    	public function display() {
    		
    		if ($this->select($this->query, $this->where, $this->order)) {
    		
    			echo '<h3>' . $this->lastQuery . '</h3><br />';
    		
    			while ($row = mysql_fetch_array($this->result, MYSQL_ASSOC)) {
    				echo '<b>' . $row['id'] . ' </b>' . $row['nome'] . '<br />';
    			}
    			
    			return true;
    			
    		}
    		
    		return false;
    	
    	}
    E richiamarla cono varie tabelle con campi diversi come potrei fare, nel senso mi spiego, vorrei fare in modo che la funzione display sia generica, nel senso da usare una sola con varie tabelle, come potrei modificarla e poi magari nella funzione passarle i campi, così da avere una funzione generica pronta per essere usate per tutte le select? spero di essermi spiegato bene
    Ultima modifica di Werwolfe; 06-02-2014 a 14:25

  5. #5
    Mi sa che devi creare una funzione per ogni tabella, dal momento che ogni tabella ha campi diversi, a meno che non vuoi stampare tutti i campi iterando sui campi dell'array associativo.

  6. #6
    Me lo studio vediamo che ne viene fuori grazie

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.