Einzelnen Beitrag anzeigen
Alt 15.09.2008, 08:37   #6 (permalink)
Megger
Erfahrener Benutzer
 

Standard

Ich habe germerkt, dass ich mir da noch ein bisschen was anderes überlegen muss, da meine Klasse, wo ich sie verwenden will, nicht funktioniert, lokal in XAMPP funktioniert das ganze, aber nicht online. Muss ich mal schauen. Bisschen was eigenes schreiben oder so.

Edit:
Habe jetzt mal ein bisschen was eigenes geschrieben, ohne diese ganzen Exceptions usw., wer braucht sowas schon. Aber das mit dem Iterator musste einfach drin bleiben, dadurch kann ich zum Beispiel einfach sagen:
PHP-Code:
foreach($result as $row) {
 

usw.
PHP-Code:
<?php
 
class MegResult implements Iterator {
  private 
$numRows 0;
  private 
$pointer 0;
  private 
$rows = array();
 
  function 
__construct($result) {
   
$rows = array();
   
$this->numRows 0;
   while(
$row = @mysql_fetch_assoc($result)) {
    
$rows[] = $row;
    ++ 
$this->numRows;
   }
   
$this->rows $rows;
  }
 
  function 
rewind() {
   
$this->pointer 0;
  }
 
  function 
key() {
   return 
$this->pointer;
  }
 
  function 
current() {
   if(
$this->pointer<count($this->rows)) {
    return 
$this->rows[$this->pointer];
   }
   return 
false;
  }
 
  function 
valid() {
   return (bool) 
is_array($this->current());
  }
 
  function 
hasNext() {
   if(
$this->pointer+1<count($this->rows)) {
    return 
true;
   }
   return 
false;
  }
 
  function 
next() {
   
$this->pointer++;
  }
 
  function 
get_num_rows() {
   return 
$this->numRows;
  }
 }
 class 
MegDB {
  private 
$ressource;
  private 
$dbhost;
  private 
$dbuser;
  private 
$dbpass;
  private 
$dbschema;
  private 
$dbprefix;
  private 
$connected false;
  private 
$statements = array();
  private 
$lastStatement;
  private 
$numQuerys 0;
  private 
$numRows 0;
 
  function 
__construct($dbhost,$dbuser,$dbpass,$dbschema,$dbprefix) {
   
$this->dbhost $dbhost;
   
$this->dbuser $dbuser;
   
$this->dbpass $dbpass;
   
$this->dbschema $dbschema;
   
$this->dbprefix $dbprefix;
   if(
$this->connect()) {
    if(
$this->select()) {
     return 
true;
    }
   }
   return 
false;
  }
 
  function 
__destruct() {
   if(
$this->connected) {
    
mysql_close($this->ressource);
   }
  }
 
  function 
connect() {
   
$this->ressource = @mysql_connect($this->dbhost,$this->dbuser,$this->dbpass);
   if(
$this->ressource=='') {
    return 
false;
   }
   return 
true;
  }
 
  function 
select() {
   if(
mysql_select_db($this->dbschema,$this->ressource)) {
    
$this->connected true;
    return 
true;
   }
   return 
false;
  }
 
  function 
execute($sql,$sqltype="select") {
   
$result mysql_query($sql,$this->ressource);
   if(
$result!=false) {
    
array_push($this->statements,array("statement"=>$sql,
               
"erfolg"=>1,
               
"error"=>null));
    
$this->lastStatement $this->statements[count($this->statements)-1];
    
$this->numQuerys++;
    switch(
$sqltype) {
     case 
"select":
      
$this->numRows = @mysql_num_rows($result);
      break;
     case 
"insert":
     case 
"update":
     case 
"delete":
      
$this->numRows = @mysql_affected_rows($this->ressource);
      break;
    }
    return 
$result;
   }
   
array_push($this->statements,array("statement"=>$sql,
              
"erfolg"=>0,
              
"error"=>mysql_errno().": ".mysql_error()));
   
$this->lastStatement $this->statements[count($this->statements)-1];
   return 
false;
  }
 
  function 
query($sql) {
   if(
$this->connected) {
    
$result $this->execute($sql);
    return new 
MegResult($result);
   }
   return 
false;
  }
 
  function 
insert($sql) {
   if(
$this->connected) {
    
$result $this->execute($sql,"insert");
    if(
$result!=false) {
     return 
mysql_insert_id($this->ressource);
    }
   }
   return 
false;
  }
 
  function 
update($sql) {
   if(
$this->connected) {
    
$result $this->execute($sql,"update");
    if(
$result!=false) {
     return 
true;
    }
   }
   return 
false;
  }
 
  function 
getRessource() {
   return 
$this->ressource;
  }
 
  function 
countQuerys() {
   return 
$this->numQuerys;
  }
 
  function 
get_num_rows() {
   return 
$this->numRows;
  }
 
  function 
getStatements() {
   return 
$this->statements;
  }
 
  function 
getErrorStatements() {
   
$errorStatements = array();
   foreach(
$this->statements as $statement) {
    if(
$statement['erfolg']==0) {
     
array_push($errorStatements,$statement);
    }
   }
   return 
$errorStatements;
  }
 
  function 
clearStatements() {
   
$this->statements = array();
   
$this->lastStatement NULL;
  }
 
  function 
getLastStatement() {
   return 
$this->lastStatement;
  }
 
  function 
isConnected() {
   if(
$this->connected) {
    return 
true;
   }
   return 
false;
  }
 
  function 
escapeString($input) {
   return 
mysql_real_escape_string($input,$this->ressource);
  }
 
  function 
escapeArray($input) {
   for(
$i=0;$i<count($input);$i++) {
    
$input[$i] = $this->escapeString($input[$i]);
   }
   return 
$input;
  }
 }
?>

Geändert von Megger (16.09.2008 um 08:50 Uhr).
Megger ist offline   Mit Zitat antworten An Facebook senden