$value ) { $this->{$parameter} = $value; } // Laad de connectie gegevens uit een bestand // !! LET OP: niet veilig, gebruiker kan dit bestand ook zien. Beveilingen met .htaccess of andere aanpak bedenken } elseif ( is_file ( $arg_param ) ) { if (! $handle = file ( $arg_param ) ) { trigger_error('Invalid path parameter given for connection'); } else { foreach ($handle as $line) { $arr = explode('=',$line); $this->$arr[0] = trim($arr[1]); } } } else { trigger_error('No connection parameters provided',E_USER_ERROR); } // Maak de connectie naar de database $this->connect(); // Er zijn nog geen queries uitgeoverd $query_counter = 0; } public function __destruct() { } public static function getInstance( $arg_param ) { /* pre: - post: Geeft instantie van huidig object, altijd maar 1 instantie te gelijk ( SINGLETON ) */ static $instance; if( ! isset( $instance ) ){ $object= __CLASS__; $instance = new $object($arg_param); } return $instance; } public function get_con_id() { return $this->con_id; } public function get_host() { return $this->host; } public function get_user() { return $this->user; } public function get_password() { return $this->password; } public function get_database() { return $this->database; } private function connect() { /* pre: - post: Huidige connectie wordt gesloten en nieuwe connectie is aangamaakt */ // Sluit huidige connectie als die er is. if ( $this->con_id ) { mysql_close($this->con_id); $this->con_id = NULL; } // Alles moet wel zijn ingevuld if ( ($this->host == '') || ($this->user == '') || ($this->password == '') || ($this->database == '') ){ trigger_error('Insufficient connection parameters given',E_USER_ERROR); } else { if ( ! $this->con_id = mysql_connect($this->host,$this->user,$this->password ) ) { trigger_error('Unable to create connection',E_USER_ERROR); } else { if ( ! mysql_select_db($this->database,$this->con_id) ) { trigger_error('Error selecting database',E_USER_ERROR); } } } } public function query($arg_sql) { /* pre: - post: Een clsResult wordt geretured met de huidige query Querycounter is met 1 verhoogd */ if ( ! $result = mysql_query($arg_sql, $this->con_id) ) { trigger_error('Error performing query, error given: ' .mysql_error($this->con_id) . ' on query: ' .$arg_sql,E_USER_ERROR); } $this->queries++; return new clsResult($this, $result); } public function quote_smart( $arg_value ) { /* pre: - post: $arg_value wordt ge-escaped. Werkt ook met magic_quotes */ if (get_magic_quotes_gpc()) { $arg_value = stripslashes($arg_value); } // Quote if not integer $arg_value = mysql_real_escape_string($arg_value ); return $arg_value; } } class clsResult { protected $myparent; // clsDatabase protected $result; public function __construct(clsDatabase $arg_parent, $arg_query ) { $this->myparent = $arg_parent; $this->result = $arg_query; } public function __destruct() { // Geef resources van query vrij, maar alleen als het een resource is if ( is_resource($this->result) ) { mysql_free_result($this->result); } } public function CountRows() { /* pre: $this->result is resource post: Het aantal rijen in result, false voor 0 rijen */ $rows = mysql_num_rows($this->result); if ( $rows == 0 ){ return false; } return $rows; } function fetchRow(){ /* pre: $this->result is resource post: Geef een array van de huidige rij */ return mysql_fetch_array($this->result,MYSQL_ASSOC); } public function CountAffectedRows() { /* pre: $this->result is GEEN resource, maar resultaat van UPDATE of INSERT post: Geeft het aantal aangepaste rijen weer, false voor 0 rijen */ $rows = mysql_affected_rows($this->myparent->get_con_id()); if ($rows == 0) { return false; } else { return $rows; } } public function getInsertId() { /* pre: $this->result is GEEN resource, maar resultaat van UPDATE of INSERT post: Geeft het id van de laatst ingevoegde rij */ if( ! $id = mysql_insert_id( $this->myparent->get_con_id() ) ){ trigger_error('Error getting ID',E_USER_ERROR); } return $id; } public function seekRow( $arg_row = 0 ) { /* pre: $this->result is resource, 0 =< $arg_row =< count($this->result) post: Zoek een rij in het resultaat */ if( ! mysql_data_seek($this->result,$arg_row)){ trigger_error('Error seeking data',E_USER_ERROR); } } } ?>