Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2002
    Messaggi
    115

    classi prepare() bind_param() come passargli i dati

    Buongiorno a tutti, ogni tanto ritorna la voglia di provare a fare cose diverse e molte volte da soli non si riesce.

    Come da titolo volevo utilizzare le classi usando prepare() dind_param() execute(), ho provato per 2 giorni senza ottenere risultati. Ho trovato una class che utilizza questi comandi ma non so come passargli e ottenere i dati.

    Codice PHP:
            public function insert($table$data$format) {
                
    // Check for $table or $data not set
                
    if ( empty( $table ) || empty( $data ) ) {
                    return 
    false;
                }
                
                
    // Connect to the database
                
    $db $this->connect();
                
                
    // Cast $data and $format to arrays
                
    $data = (array) $data;
                
    $format = (array) $format;
                
                
    // Build format string
                
    $format implode(''$format);
                
    $format str_replace('%'''$format);
                
                list( 
    $fields$placeholders$values ) = $this->prep_query($data);
                
                
    // Prepend $format onto $values
                
    array_unshift($values$format);

                
    // Prepary our query for binding
                
    $stmt $db->prepare("INSERT INTO {$table} ({$fields}) VALUES ({$placeholders})");

                
    // Dynamically bind values
                
    call_user_func_array( array( $stmt'bind_param'), $this->ref_values($values));
                
                
    // Execute the query
                
    $stmt->execute();
                
                
    // Check for successful insertion
                
    if ( $stmt->affected_rows ) {
                    return 
    true;
                }
                
                return 
    false;
            }

        public function 
    query($query) {
            
    $db $this->connect();
            
    $result $db->query($query);
                
            while ( 
    $row $result->fetch_object() ) {
                
    $results[] = $row;
            }
                
            return 
    $results;
        } 
    su $table non ho dubbi
    su $data e $format non saprei come mettere i dati
    nessuna idea su $results[] = $row;

    Codice PHP:
            public function update($table$data$format$where$where_format) {
                
    // Check for $table or $data not set
                
    if ( empty( $table ) || empty( $data ) ) {
                    return 
    false;
                }
                
                
    // Connect to the database
                
    $db $this->connect();
                
                
    // Cast $data and $format to arrays
                
    $data = (array) $data;
                
    $format = (array) $format;
                
                
    // Build format array
                
    $format implode(''$format); 
                
    $format str_replace('%'''$format);
                
    $where_format implode(''$where_format); 
                
    $where_format str_replace('%'''$where_format);
                
    $format .= $where_format;
                
                list( 
    $fields$placeholders$values ) = $this->prep_query($data'update');
                
                
    //Format where clause
                
    $where_clause '';
                
    $where_values '';
                
    $count 0;
                
                foreach ( 
    $where as $field => $value ) {
                    if ( 
    $count ) {
                        
    $where_clause .= ' AND ';
                    }
                    
                    
    $where_clause .= $field '=?';
                    
    $where_values[] = $value;
                    
                    
    $count++;
                }

                
    // Prepend $format onto $values
                
    array_unshift($values$format);
                
    $values array_merge($values$where_values);

                
    // Prepary our query for binding
                
    $stmt $db->prepare("UPDATE {$table} SET {$placeholders} WHERE {$where_clause}");
                
                
    // Dynamically bind values
                
    call_user_func_array( array( $stmt'bind_param'), $this->ref_values($values));
                
                
    // Execute the query
                
    $stmt->execute();
                
                
    // Check for successful insertion
                
    if ( $stmt->affected_rows ) {
                    return 
    true;
                }
                
                return 
    false;
            }
            public function 
    select($query$data$format) {
                
    // Connect to the database
                
    $db $this->connect();
                
                
    //Prepare our query for binding
                
    $stmt $db->prepare($query);
                
                
    //Normalize format
                
    $format implode(''$format); 
                
    $format str_replace('%'''$format);
                
                
    // Prepend $format onto $values
                
    array_unshift($data$format);
                
                
    //Dynamically bind values
                
    call_user_func_array( array( $stmt'bind_param'), $this->ref_values($data));
                
                
    //Execute the query
                
    $stmt->execute();
                
                
    //Fetch results
                
    $result $stmt->get_result();
                
                
    //Create results object
                
    while ($row $result->fetch_object()) {
                    
    $results[] = $row;
                }

                return 
    $results;
            }
            public function 
    delete($table$id) {
                
    // Connect to the database
                
    $db $this->connect();
                
                
    // Prepary our query for binding
                
    $stmt $db->prepare("DELETE FROM {$table} WHERE ID = ?");
                
                
    // Dynamically bind values
                
    $stmt->bind_param('d'$id);
                
                
    // Execute the query
                
    $stmt->execute();
                
                
    // Check for successful insertion
                
    if ( $stmt->affected_rows ) {
                    return 
    true;
                }
            }
            private function 
    prep_query($data$type='insert') {
                
    // Instantiate $fields and $placeholders for looping
                
    $fields '';
                
    $placeholders '';
                
    $values = array();
                
                
    // Loop through $data and build $fields, $placeholders, and $values            
                
    foreach ( $data as $field => $value ) {
                    
    $fields .= "{$field},";
                    
    $values[] = $value;
                    
                    if ( 
    $type == 'update') {
                        
    $placeholders .= $field '=?,';
                    } else {
                        
    $placeholders .= '?,';
                    }
                    
                }
                
                
    // Normalize $fields and $placeholders for inserting
                
    $fields substr($fields0, -1);
                
    $placeholders substr($placeholders0, -1);
                
                return array( 
    $fields$placeholders$values );
            }
            private function 
    ref_values($array) {
                
    $refs = array();

                foreach (
    $array as $key => $value) {
                    
    $refs[$key] = &$array[$key]; 
                }

                return 
    $refs
            } 
    In sostanza tutto quello che viene passato dentro le parentesi tonde ($table, $id) non so come metterlo

    e vorrei capire come visualizzare i dati provenienti da select.

    Ringrazio tutti e capisco se qualcuno mi manderà....

  2. #2
    Dove hai preso le funzioni ci sarà pure un esempio del loro utilizzo! Basta controllare nel codice che le invoca per vedere che valori vengono passati.
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

  3. #3
    Utente di HTML.it
    Registrato dal
    Dec 2002
    Messaggi
    115
    Non c'è nessun esempio per utilizzarlo.

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.