Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 19

Discussione: inserire l id con pdo

Hybrid View

  1. #1
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    1,805

    inserire l id con pdo

    salve ragazzi in una classe per l utilizzo di PDO ho aggiunto la funzione insert_id ma mi restituisce sempre 0 perche ? posto la classe
    Codice PHP:


    class cbSQLRetrieveData {
        
        
    }

    class 
    cbSQLConnectVar {
        
        const 
    DB_DEFAULT 0x000000;
        const 
    DB_MYSQL   0x000001;
        const 
    DB_POSTGRE 0x000005;
        const 
    DB_SQLITE  0x000010;
        
        const 
    FETCH_ASSOC =  15;
        const 
    FETCH_LAZY  =  25;
        const 
    FETCH_OBJECT 35;
    }
    class 
    cbSQLConnect {
        
        public 
    $LastQuery// record last query (temporary)
        
    private $db// stores  setup class and all database related like tablenames, database name and connection string
        
    private $data// no use when i updated  query run mode
        
    private $Rows// store number of rows affected
        
    private $FetchType// stores type of fetch result of querys 
        
    private $PDOInstance// class global PDO Instance 
        
        
    public function __construct(cbSQLConnectConfig &$setup$mode cbSQLConnectVar::FETCH_ASSOC){
            
    $this->db = &$setup;
            
    $this->FetchType $mode;
            
    $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODEPDO::FETCH_FUNC);
            
    $this->PDOInstance cbSQLConnectConfig::CreatePDOInstance();
        }
        public function 
    escape($String)
        {
                return 
    addcslashes(str_replace('\\'"\\"addslashes($String)), '');
        }

        public function 
    QuerySingle($query){
            
    $instance cbSQLConnectConfig::CreatePDOInstance();
            
    $stmt $instance->prepare($query);
                switch( 
    $this->FetchType){
                    case 
    cbSQLConnectVar::FETCH_ASSOC:                
                        
    $tp $this->RunQuery($stmt);
                        
    $this->LastQuery $query;
                        break;
                    case 
    cbSQLConnectVar::FETCH_LAZY:
                        
    $this->LastQuery $query;
                        
    $tp $this->RunQueryAsLazy();
                        break;
                    case 
    cbSQLConnectVar::FETCH_OBJECT:
                        
    $this->LastQuery $query;
                        
    $tp $this->RunQueryAsObject();
                        break;
                    default:
                        
    $this->LastQuery $query;
                        
    $stmt $this->db->prepare($query);    
                        
    $tp $this->RunQuery($stmt);
                        break;
            }
            return 
    $tp;
        }
        public function 
    Query($query$binds =''){
                
    $stmt $this->db->prepare($query);
                
    $binds = isset($binds)?$binds:'';
             if(!empty(
    $binds))
            {    
                  foreach(
    $binds as $bindKey => $key)
                {
                
    $stmt->bindParam($bindKey$keyPDO::PARAM_STRPDO::PARAM_INT);
                }
            }
              return 
    $this->RunQuery($stmt);  
        }
          public function 
    insert_id()
        {
            return  
    $this->db->lastInsertID(); 
        }
        
         private function 
    RunQuery(PDOStatement $state$param  NULL)
        {
            try
              {
                 if(empty(
    $param) || $param == NULL)
                {
            
                
    $state->execute();
                
    $this->data =  $state->fetchAll(PDO::FETCH_ASSOC);
                
    $this->Rows $state->rowCount();
                
    $state->closeCursor(); 
                        return 
    $this->data;
                }else {
                
                
    $state->execute($param);
                
    $this->data =  $state->fetchAll(PDO::FETCH_ASSOC);
                
    $this->Rows $state->rowCount();
                
    $state->closeCursor(); 
                        return 
    $this->data;
                }
              }
              catch(
    PDOException $ex)
                                    {
                
                throw new 
    PDOException($ex->getMessage());
                    
            }
        }
        
        private function 
    RunQueryAsObject(){
            
    $stmt $this->db->query($this->LastQuery);
            
    $result $stmt->fetchAll(PDO::FETCH_CLASS"cbSQLRetrieveData");
                    return 
    $result;
        }
        private function 
    RunQueryAsLazy(){
            
            
    $stmt $this->db->query($this->LastQuery);
            
    $result $stmt->fetchAll(PDO::FETCH_BOTH);
            return 
    $result;
        }
      private function 
    implode2($glue1$glue2$array){
      return ((
    sizeof($array) > 2)? implode($glue1array_slice($array0, -2)).$glue1 "").implode($glue2array_slice($array, -2));
    }
        private function 
    addQuotes($word$doublequotes true){
            
            if(
    $doublequotes){
            
    $pword '"'.$word.'"';    
            }else{
            
    $pword =  "'".$pword."'";    
            }
                
            return 
    $pword;
        }
        
         public function 
    SQLInsert($array$table)
        {
             if(empty(
    $table))
            {
                 return 
    false;
            }
        
    $inst cbSQLConnectConfig::CreatePDOInstance();
        
    $fieldname array_keys($array[0]);
        
    $table sprintf("INSERT INTO %s" $table);
        
    $fields  '('.implode(",",$fieldname).')';
        
    $placeholder 'VALUES(:'.implode(", :"$fieldname).')';
        
    $query sprintf("%s %s %s"$table$fields$placeholder);
        
    $stmt $inst->prepare($query);
             foreach(
    $array[0]  as $param => $key)
            {
                
    $stmt->bindValue(':'.$param,  $array[0][$param]);    
            }
                
    $stmt->execute();
        if(
    $stmt->rowCount() != 0){
        
    $stmt->closeCursor();
        return 
    true;
        }else {
        
    $stmt->closeCursor();
        return 
    false;
        }
        }
        public function 
    SQLUpdate($table$fieldToChange$fieldValue$idField$idValue){
                
                
    $query sprintf("UPDATE %s SET %s = :value WHERE %s = :comparevalue"$table$fieldToChange$idField);
                
    $stmt $this->db->prepare($query);
                if(!
    $stmt){
                    
    print_r$stmt->errorInfo());
                    return 
    false;
                }
                
    $stmt->bindParam(":value",  $fieldValuePDO::PARAM_INT PDO::PARAM_STR);
                
    $stmt->bindParam(":comparevalue",$idValuePDO::PARAM_INT PDO::PARAM_STR);
                
    $stmt->execute();
                if(
    $stmt->rowCount() != 0){
                    
    $stmt->closeCursor();
                    return 
    true;
                }
                
    $stmt->closeCursor();
                return 
    false;
        }
        public function 
    SQLDelete($table$idTable$idValue){
            
    $inst cbSQLConnectConfig::CreatePDOInstance();
            
    $query sprintf("DELETE FROM %s WHERE %s = :value"$table$idTable);
            
    $stmt $inst->prepare($query);
            
    $stmt->bindParam(":value"$idValue);
            
    $stmt->execute();
            
    $stmt->closeCursor();
        }
        
        public function 
    RowsAffected(){
            return 
    $this->Rows;    
        }
    }
    class 
    cbSQLConnectConfig extends PDO {
        
             protected 
    $actualStatement;
            protected static 
    $qs;
            protected static 
    $user;
            protected static 
    $pwd;
            protected static 
    $options
            protected static 
    $fetchMode;
             protected static 
    $driverDB;
            
            public function 
    __construct($driver$host$port 3306$database$user$pwd$options = array()){
                
                
    cbSQLConnectConfig::$qs =  $this->ConnectionStringGenerator($driver$host$port$database);
                
    cbSQLConnectConfig::$user $user;
                
    cbSQLConnectConfig::$pwd $pwd;
                
    cbSQLConnectConfig::$options $options;
                
    parent::__construct(cbSQLConnectConfig::$qscbSQLConnectConfig::$usercbSQLConnectConfig::$pwdcbSQLConnectConfig::$options);
            }
            
            public static function 
    CreatePDOInstance(){
                return new 
    PDOcbSQLConnectConfig::$qs,  cbSQLConnectConfig::$user,  cbSQLConnectConfig::$pwd,  cbSQLConnectConfig::$options);
                
            }
            
            protected function 
    getConnectionString(){
                return 
    $this->qs;
            }
            
            public function 
    getTable(){
                
    preg_match("/dbname=([aA-zZ0-9]+)/"$this->qs$mt);
                return 
    $mt[1];
            }
            
            private  function 
    ConnectionStringGenerator($driver$host$port$database){
                
                switch( (int) 
    $driver){
                
                 case 
    cbSQLConnectVar::DB_MYSQL:
                 
    cbSQLConnectConfig::$driverDB "mysql";
                 break;
                 case 
    cbSQLConnectVar::DB_POSTGRE:
                 
    cbSQLConnectConfig::$driverDB "psgre";
                 default:
                 
    cbSQLConnectConfig::$driverDB "mysql";
                 break;
                }
                
    $connectionstring sprintf("%s:host=%s;port=%d;dbname=%s"cbSQLConnectConfig::$driverDB$host$port$database);
                return 
    $connectionstring;
            }
    }
            if(
    defined("DATABASE_HOST")){$dbhost DATABASE_HOST ; }
            if(
    defined("DATABASE_USER")){$db_user DATABASE_USER ; }
            if(
    defined("DATABASE_PASSWORD")){$db_pass DATABASE_PASSWORD ; }
            if(
    defined("DATABASE_NAME")){$db_name DATABASE_NAME ; }
            if(
    defined("DATABASE_PORT")){$db_port DATABASE_PORT ; }
            if(
    defined("PCONNECT")){$db_persistent PCONNECT ; }
            
    $databaseSetup = new cbSQLConnectConfigcbSQLConnectVar::DB_MYSQL$dbhost,$db_port,$db_name,$db_user,'');  
    $dbcore = new cbSQLConnect($databaseSetupcbSQLConnectVar::FETCH_ASSOC); 
    Non è tanto importante saper fare,quanto ad avere voglia d imparare .

  2. #2
    Utente di HTML.it L'avatar di clasku
    Registrato dal
    Aug 2006
    Messaggi
    3,197
    come la richiami?

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    1,805
    prima inserisco la query nel database
    Codice PHP:
      function sendNewTicket($ticketmaskid,$departmentid,$statusid ,$priorityid,$name,$email,$subjet,$tickettext)
    {
     global 
    $dbcore $RCW 
     
    $userid = isset($RCW['user']['userid'])?$RCW['user']['userid']:0;
    $res =  $dbcore->SQLInsert(array(array(
                                           
    "ticketpostsid"=>"NULL" 
                                           
    "ticketmaskid"=>$ticketmaskid 
                                           
    "departmentid"=>$departmentid
                                           
    "statusid"=>
                                           
    "priorityid"=>$priorityid 
                                           
    "emailqueueid"=>
                                           
    "dateline"=>time() ,
                                           
    "userid"=>$userid 
                                           
    "name"=>$name 
                                           
    "email"=>$email 
                                           
    "subjet"=>$subjet ,
                                           
    "edited"=>"" 
                                           
    "editedby"=>"" 
                                           
    "editeddateline"=>"" )
                                           ),
                                           
    "ticketposts");
                                    
    $insertid =      $dbcore->insert_id(); 
                                    
    print_r($dbcore->insert_id());
           if(! 
    $res)
          {
             return 
    false ;
          }

                                    
                                    
    $resdata =  $dbcore->SQLInsert(array(array(
                                           
    "ticketdataid"=>"NULL" 
                                           
    "ticketid"=>$insertid 
                                           
    "tickettext"=>$tickettext
                                           
    "dateline"=>time())
                                           ),
                                           
    "ticketposts");
                                           
           if(! 
    $resdata)
          {
             return 
    false ;
          }
         return 
    true


    Non è tanto importante saper fare,quanto ad avere voglia d imparare .

  4. #4
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    1,805
    buongiorno nessun suggerimento?
    Non è tanto importante saper fare,quanto ad avere voglia d imparare .

  5. #5
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    1,805
    ciao $dbcore->insert_id();
    Non è tanto importante saper fare,quanto ad avere voglia d imparare .

  6. #6
    Quote Originariamente inviata da rocco.mod Visualizza il messaggio
    ciao $dbcore->insert_id();
    scusate ma la classe cbSqlConnect non ha nessun metodo "insert_id()" definito quindi
    Ultima modifica di Al_katraz984; 02-11-2014 a 18:50
    Questa volta, più che un voto.. è favoreggiamento.

  7. #7
    Utente di HTML.it L'avatar di clasku
    Registrato dal
    Aug 2006
    Messaggi
    3,197
    Si, OK... Ma servirebbe capire cosa fai prima

  8. #8
    Utente di HTML.it L'avatar di clasku
    Registrato dal
    Aug 2006
    Messaggi
    3,197
    la query va a buon fine? i dati nel db li vedi? io vedo un NULL da inserire per ticketpostsid che mi fa pensare che non sia un campo autoincrement (io non ce lo metterei né tra i nomi tabella né nei valori da inserire)

  9. #9
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    1,805
    ticketpostsid e autoincrement
    Non è tanto importante saper fare,quanto ad avere voglia d imparare .

  10. #10
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    1,805
    no ho provato ad eliminare la funziome colsedcursor ma ugualmente mi ritorna 0 come id
    Non è tanto importante saper fare,quanto ad avere voglia d imparare .

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