per quanto mi riguarda io faccio cosi..
Codice PHP:
/**
* Initialize a database connection.
*/
private function connect() {
$url = parse_url( parent:: DB_URL );
........
$connection = @mysql_connect( $url['host'], $url['user'], $url['pass'], TRUE, 2);
if ( !$connection || !mysql_select_db( substr( $url['path'], 1) ) ) {
// Show error screen otherwise
throw new Exception( __CLASS__ . ': ' . mysql_error() );
}
// Force UTF-8.
mysql_query( 'SET NAMES "utf8"', $connection );
return $connection;
}
/**
* @return
* resource, TRUE, FALSE
*/
public function dbQuery() {
if ( !mysql_errno( $this->connect() ) ) {
return mysql_query( $this->query, $this->connect() );
} else {
$error = mysql_error( $this->connect() ) . "\nquery: " . $this->query;
throw new Exception( __CLASS__ . ': ' . $error, 1 );
}
}
esempio
/*
* @return
* Un array di oggetti con tutte le righe selezionate
*/
public function fetchObjectAll() {
$arr = array();
$result = $this->dbQuery();
if ( $result ) {
while( $obj = mysql_fetch_object( $result ) ) {
$arr[] = $obj;
}
return $arr;
} else {
return FALSE;
}
}
un metodo per la connessione (mancano parti) e un metodo per l'esecuzione della query.. l'ultimo è un esempio di metodo custom per richiamare i dati..