openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit ef42834cf5345963f3f38061b3e105789ada1864
parent 93d661305cd69d8065ba7ae60586412d10565129
Author: dankert <devnull@localhost>
Date:   Mon, 18 Jan 2010 23:26:07 +0100

PDO-Schnittstelle arbeitet nun mit Prepared Statements

Diffstat:
Mdb/pdo.class.php | 55+++++++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 47 insertions(+), 8 deletions(-)

diff --git a/db/pdo.class.php b/db/pdo.class.php @@ -41,6 +41,8 @@ class DB_pdo * @var String */ var $error; + + var $prepared = false; function connect( $conf ) @@ -77,21 +79,41 @@ class DB_pdo function query($query) { - $this->result = $this->connection->query($query); + if ( $this->prepared ) + { + $ar = array(); + + foreach( $query->data as $val ) + $ar[] = $val['value']; + $erg = $this->stmt->execute( $ar ); - if ( ! $this->result ) + if ( $erg === false ) + { + die( 'Could not execute prepared statement "'.$query->query.'" with values "'.implode(',',$ar).'": '.implode('/',$this->connection->errorInfo()) ); + } + + return $this->stmt; + } + else { - $this->error = 'Database error: '.PDO::errorInfo(); - return FALSE; + $this->result = $this->connection->query($query); + + if ( ! $this->result ) + { + $this->error = 'Database error: '.implode('/',$this->connection->errorInfo()); + return FALSE; + } + return $this->result; } - - return $this->result; } function fetchRow( $result, $rownum ) { - return $this->result->fetch( PDO::FETCH_ASSOC ); + if ( $this->prepared ) + return $this->stmt->fetch( PDO::FETCH_ASSOC ); + else + return $this->result->fetch( PDO::FETCH_ASSOC ); } @@ -103,16 +125,22 @@ class DB_pdo function prepare( $query,$param) { + $offset = 0; foreach( $param as $pos) { foreach( $pos as $posx ) { - $query = substr($query,0,$posx-1).'?'.substr($query,$posx+1); + $posx += $offset++; + $query = substr($query,0,$posx).'?'.substr($query,$posx); } } + $this->prepared = true; $this->stmt = $this->connection->prepare($query); + if ( $this->stmt === false ) + die( 'Database error: '.implode('/',$this->connection->errorInfo()) ); + } @@ -152,6 +180,17 @@ class DB_pdo } + + /** + * Setzt die letzte Abfrage zurueck. + */ + function clear() + { + $this->prepared = false; + $this->params = array(); + } + + /** * Why this? See http://e-mats.org/2008/07/fatal-error-exception-thrown-without-a-stack-frame-in-unknown-on-line-0/ *