Mysql
 sql >> Database >  >> RDS >> Mysql

estendendo la classe DOP

$dsn è il nome dell'origine dati. Gestisce il tuo nome host per te. Lo usi in questo modo:

$dsn = 'mysql:dbname=YOUR_DB_NAME;host=YOUR_HOSTNAME'

Con la riga $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); Hai impostato le eccezioni da sollevare quando si verificano errori (cosa che mi piace), quindi nella tua classe estesa puoi gestire gli errori nei gestori di eccezioni. Se avessi un metodo chiamato getAssoc nella tua classe PDO estesa, sarebbe simile a questo:

/// Get an associative array of results for the sql.
public function getAssoc($sql, $params=array())
{
   try
   {
      $stmt = $this->prepare($sql);
      $params = is_array($params) ? $params : array($params);
      $stmt->execute($params);

      return $stmt->fetchAll(PDO::FETCH_ASSOC);
   }
   catch (Exception $e)
   {
      // Echo the error or Re-throw it to catch it higher up where you have more
      // information on where it occurred in your program.
      // e.g echo 'Error: ' . $e->getMessage(); 

      throw new Exception(
            __METHOD__ . 'Exception Raised for sql: ' . var_export($sql, true) .
            ' Params: ' . var_export($params, true) .
            ' Error_Info: ' . var_export($this->errorInfo(), true),
            0,
            $e);
   }
}