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

    [PHP] Oop applicata a query di selezione

    Salve, premetto che sto studiando le classi davvero da pochi giorni. Questo è il file config dove sono segnate le classi del blog:
    Codice PHP:
    <?php

    class blog
        private 
    $db_host
        private 
    $db_user
        private 
    $db_pass
        private 
    $db_name
        public 
    $conn
        public 
    $sel
         
        public function 
    __construct(){ 
            
    $this->db_host='localhost'
            
    $this->db_user='root'
            
    $this->db_pass=''
            
    $this->db_name='login'
            
    $this->conn=mysql_connect($this->db_host,$this->db_user,$this->db_pass); 
            
    $this->sel=mysql_select_db($this->db_name,$this->conn);
        } 
         
    }class 
    conn extends blog
         
        public function 
    Errore_connessione(){ 
            if(!
    $this->conn){ 
                die(
    'Errore di connessione'); 
            }
        } 
             
            public function 
    Errore_selezione(){ 
            if(!
    $this->sel){ 
                die(
    'Errore di connessione del database');
             
              }
             }
            }class 
    insert extends conn{
                public 
    $post;
                public 
    $utente;
                public 
    $testo;
                        
                public function 
    InsertCommenti(){
                    if(isset(
    $this->conn)){    
                    
    $this->post=@$_SESSION['post'];
                    
    $this->utente=@$_SESSION['utente'];
                    
    $this->testo=@$_POST['testo'];
                                
                        
    $query="INSERT INTO commenti (post, utente, testo) VALUES ('{$this->post}','{$this->utente}','{$this->testo}')";
                        
    $result=mysql_query($query$this->conn) or die ("Errore nella query ".mysql_error());
                    }else{
                        die(
    'Errore nella query');
                    }
                }
            }class 
    select extends insert{
                public 
    $query;
                public 
    $result;
                
                public function 
    __construct(){
                    
    $this->query="SELECT*FROM commenti WHERE post='{$this->post}'";
                    
    $this->result=mysql_query($this->query$this->conn) or die ("Errore: ".mysql_error());
                }
                
                public function 
    SelectCommenti(){
                    if(isset(
    $this->conn)){
                        
    $this->query;
                        
    $this->result;
                    }else{
                        die(
    'Errore nella selezione '.mysql_error());
                    }
                }
            }
        
        
        
        

    $conn = new blog(); 
    $conn->conn

    $sel= new blog(); 
    $sel->sel

    $Errore_connessione = new conn(); 
    $Errore_connessione->Errore_connessione(); 

    $Errore_selezione = new conn(); 
    $Errore_selezione->Errore_selezione();
    ?>
    La query di inserzione dei commenti funziona ed è la seguente:
    Codice PHP:
    <?php

    $insert 
    = new insert();
    $insert->InsertCommenti();

    include(
    'commenti_pubblicati.php');

    ?>
    Il problema è che non funziona la visualizzazione dei commenti, programmata nel file 'commenti_pubblicati.php', ch'è il seguente:
    Codice PHP:
    <?php
    $select 
    = new select();
    $select->SelectCommenti();

    $result = new select();
    $result->result;

    echo(
    '
    <table width=700px;>
    '
    );
    while(
    $assoc=mysql_fetch_assoc($result->result))
    {
        echo(
    '
        <tr><td>di '
    .$assoc['utente'].'</td><td>il '.$assoc['data'].'</td></tr>
        <tr><td colspan="2">'
    .$assoc['testo'].'</td></tr>
    '
    );
    }

    echo(
    '
    </table>
    '
    );

    ?>
    Mi dice "Warning: mysql_query() expects parameter 2 to be resource, null given in C:\xampp\htdocs\blog\config.php on line 57
    Errore: ".

    Questa è la linea 57:
    Codice PHP:
    $this->result=mysql_query($this->query$this->conn) or die ("Errore: ".mysql_error()); 
    Mi spiegate come devo correggere, per favore?

  2. #2
    Utente di HTML.it
    Registrato dal
    Jul 2010
    Messaggi
    719
    Penso sia dovuto al fatto che sovrascrivi il costrutto. Devi usare una classe simile all'inserimento, ovvero che non sovrascrive __construct ed esegua la query dentro la funzione SelectCommenti.


  3. #3
    Originariamente inviato da simo22
    Penso sia dovuto al fatto che sovrascrivi il costrutto. Devi usare una classe simile all'inserimento, ovvero che non sovrascrive __construct ed esegua la query dentro la funzione SelectCommenti.


    Ho corretto in questo modo:
    Codice PHP:
    class select extends insert{
                
                public function 
    SelectCommentiQuery(){
                    if(isset(
    $this->conn)){
                        
                    
    $query="SELECT*FROM commenti WHERE post='{$this->post}'";
                    
                    }else{
                        die(
    'Errore nella selezione della query '.mysql_error());
                    }
                }
                public function 
    SelectCommentiResult(){
                    if(isset(
    $this->conn)){
                    
    $result=mysql_query($this->SelectCommentiQuery(), $this->conn) or die ("Errore: ".mysql_error());
                }else{
                    die(
    'Errore di result '.mysql_error());
                }
                }
            } 
    Poi nel file commenti_pubblicati.php ho fatto così:
    Codice PHP:
    <?php
    $select 
    = new select();
    $select->SelectCommentiQuery();

    $result = new select();
    $result->SelectCommentiResult();

    echo(
    '
    <table width=700px;>
    '
    );
    while(
    $assoc=mysql_fetch_assoc($result->SelectCommentiResult()))
    {
        echo(
    '
        <tr><td>di '
    .$assoc['utente'].'</td><td>il '.$assoc['data'].'</td></tr>
        <tr><td colspan="2">'
    .$assoc['testo'].'</td></tr>
    '
    );
    }

    echo(
    '
    </table>
    '
    );

    ?>
    Ora mi dice "Errore: Query was empty".

  4. #4
    PROBLEMA RISOLTO

    nel seguente modo:
    Codice PHP:
    class selectCommenti{
        private 
    $db_host
        private 
    $db_user
        private 
    $db_pass
        private 
    $db_name
        private 
    $conn;
        private 
    $sel;
        private 
    $post;
        public 
    $query;
        public 
    $result;
        
        public function 
    __construct(){ 
            
    $this->db_host='localhost'
            
    $this->db_user='root'
            
    $this->db_pass=''
            
    $this->db_name='login'
            
    $this->conn=mysql_connect($this->db_host,$this->db_user,$this->db_pass); 
            
    $this->sel=mysql_select_db($this->db_name,$this->conn);
            
    $this->post=@$_SESSION['post'];
            
    $this->query="SELECT*FROM commenti WHERE post='{$this->post}'";
            
    $this->result=mysql_query($this->query$this->conn) or die ("Errore: ".mysql_error());
        }      

    Così funge, solo che mi piacerebbe trovare un modo più "elegante". Ci arriverò col tempo, è da nemmeno una settimana che studio la Oop.

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.