Visualizzazione dei risultati da 1 a 10 su 21

Discussione: Consiglio su PDO

Hybrid View

  1. #1
    Quote Originariamente inviata da marino51 Visualizza il messaggio
    ciao, se ti interessa posso postarti il codice che ho sviluppato per me e che uso da diverso tempo ...
    Gratis anche la polmonite Grazie Marino posta pure

  2. #2
    Utente di HTML.it
    Registrato dal
    Oct 2014
    Messaggi
    539
    Quote Originariamente inviata da camionistaxcaso Visualizza il messaggio
    Gratis anche la polmonite Grazie Marino posta pure
    Codice PHP:
    <?php
    class TheBestDB {
      private 
    $pdo;
      private 
    $inTransaction false;
      private 
    $dbNAME;
      private 
    $dbUSER;
      private 
    $dbPASS;
      public 
    $sth;
      public 
    $lastInsertId 0;

      function 
    __construct$eDBNAME=""$eDBUSER=""$eDBPASS="" )
      {
        
    $this->dbNAME $eDBNAME;
        
    $this->dbUSER $eDBUSER;
        
    $this->dbPASS $eDBPASS;
    //  $this->MyLog( "" );
    //  $this->MyLog( "main : ".basename ( $_SERVER['PHP_SELF'],".php" ).".php" );
        
    try { $this->pdo = new PDO$this->dbNAME$this->dbUSER$this->dbPASS ); }
        catch ( 
    PDOException $e ) { $this->handle_sql_errors"PDO : OPEN DB"$e ); }
        
    $this->pdo->setAttributePDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION );
        
    $this->OPtransaction false;
      } 
      public function 
    __destruct()
      { 
        
    $this->close(); 
      } 

      public function 
    queryS$sql$params=array() ) // return the retrieved dataset
      
    {
        return 
    $this->BindParams$sql$params )->fetchALLPDO::FETCH_NUM );
      } 

      public function 
    queryM$sql$params=array() ) // return the involved rows counter
      
    {
        return 
    $this->BindParams$sql$params )->rowCount();
      } 

      public function 
    queryC$sql$params=array() ) // return the retrieved element
      
    {
        return 
    current$this->BindParams$sql$params )->fetch() );
      } 

        public function 
    query$sql$params=array() ) // return $sth, the object for subsequent operation
      
    {
        return 
    $this->BindParams$sql$params );
      }

      private function 
    BindParams$sql$params=array() )
      {
        
    $this->MyLog"SQL : ".$this->pdoSqlDebug($sql$params) );
        if ( 
    substr_count$sql"?" ) != count$params ) )
          
    $this->MyErr"ERRORE : incongruenza nei parametri della query -> ".$sql );
        try { 
    $sth $this->pdo->prepare$sql, array( PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL ) );
              foreach( 
    $params as $key => &$param ) { $sth->bindParam$key 1$param ); } 
              
    $sth->execute();
              return 
    $sth; }
        catch( 
    PDOException $e ){ $this->handle_sql_errors$sql$e ); }
      } 

      public function 
    begin_transaction()
      {
        try { 
    $this->pdo->beginTransaction(); $this->OPtransaction true; }
        catch( 
    PDOException $e ){ $this->handle_sql_errors$row$e ); }
        return 
    true;
      } 
      public function 
    commit_transaction()
      {
        try { 
    $this->pdo->commit(); $this->OPtransaction false; }
        catch( 
    PDOException $e ){ $this->handle_sql_errors$row$e ); }
      } 
      public function 
    rollback_transaction()
      {
        try { 
    $this->pdo->rollBack(); $this->OPtransaction false; }
        catch( 
    PDOException $e ){ $this->handle_sql_errors$row$e ); }
      } 
      public function 
    close()
      { 
        unset( 
    $this->pdo ); 
      }
      public function 
    MyLog$text )
      {
        global 
    $swN;
        return;
        
    error_log$swN." ".$text);
      }
      public function 
    MyErr$text )
      {
        global 
    $swN;
        
    error_log$swN." ".$text);
    //  require_once '../includes/mail_SolidError.php';
        
    print "ERRORE INATTESO, contatta l'amministratore del sistema";
        die;
      }
      private function 
    handle_sql_errors$sql$e )
      {
        if ( 
    $this->OPtransaction $this->pdo->rollBack();
        
    error_log"SQL cmd    : ".$sql);
        
    error_log"error code : ".$e->getCode(), );
        
    error_log"error info : ".$e->getMessage(), );
    //  require_once '../includes/mail_SqlFatalError.php';
        
    print "ERRORE INATTESO, contatta l'amministratore del sistema";
        die;
      }
      static public function 
    pdoSqlDebug($raw_sql$parameters)
      {
        
    $keys = array();
        
    $values $parameters;
        foreach (
    $parameters as $key => $value) {
          
    // check if named parameters (':param') or anonymous parameters ('?') are used
          
    if (is_string($key)) {
            
    $keys[] = '/:'.$key.'/';
          } else {
            
    $keys[] = '/[?]/';
          }
          
    // bring parameter into human-readable format
          
    if (is_string($value)) {
            
    $values[$key] = "'" $value "'";
          } elseif (
    is_array($value)) {
            
    $values[$key] = implode(','$value);
          } elseif (
    is_null($value)) {
            
    $values[$key] = 'NULL';
          }
        }
        
    $raw_sql preg_replace($keys$values$raw_sql1$count);
        return 
    $raw_sql;
      }

    ?>

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.