Mine sisusse
Otsi siit
  • Rohkem valikuid...
Otsi tulemusi mis sisaldab...
Otsi tulemusi...

Soovitatud postitused

Kuna ma kasutan seda mitmes projektis siis ehk on mõnele teile ka kasulik.

 

class Database {

   private $con = false;
   private $result = array();
   private $querys = 0;
   private $error;

   function __construct() {

   }

   /*
    * Returns the current query count.
    */
   public function getQuerys() {
       return $this -> querys;
   }

   /*
    * Returns the latest error.
    */
   public function getErrors() {
       return $this->error;
   }

   /*
    * Connects to the database, only one connection
    * allowed
    */
   public function connect() {
       global $config;

       if (!$this -> con) {
           $myconn = @mysql_connect($config['database']['host'], $config['database']['user'], $config['database']['password']);
           if ($myconn) {
               $seldb = @mysql_select_db($config['database']['name'], $myconn);
               if ($seldb) {
                   $this -> con = true;
                   return true;
               } else {
                   $this->error = "Can't select database: " . mysql_error();
                   return false;
               }
           } else {
               $this->error = "Can't connect to MySQL Server: " . mysql_error();
               return false;
           }
       } else {
           return true;
       }
   }

   /*
    * Changes the new database, sets all current results
    * to null
    */
   public function setDatabase($name) {
       global $config;

       if ($this -> con) {
           if (@mysql_close()) {
               $this -> con = false;
               $this -> results = null;
               $config['database']['name'] = $name;
               $this -> connect();
           }
       }

   }

   /*
    * Checks to see if the table exists when performing
    * queries
    * Required: table (the name of the table)
    */
   private function tableExists($table) {
       global $config;

       $tablesInDb = @mysql_query('SHOW TABLES FROM ' . $config["database"]["name"] . ' LIKE "' . $table . '"');
       $this -> querys++;
       if ($tablesInDb) {
           if (mysql_num_rows($tablesInDb) == 1) {
               return true;
           } else {
               $this->error = "tableExists(): " . mysql_error();
               return false;
           }
       }
   }

   /*
    * Selects information from the database.
    * Required: table (the name of the table)
    * Optional: rows (the columns requested, separated by commas)
    *           where (column = value as a string)
    *           order (column DIRECTION as a string)
    */
   public function select($table, $rows = '*', $where = null, $order = null, $limit = null) {
       $this -> result = null;
       $q = 'SELECT ' . $rows . ' FROM ' . $table;
       if ($where != null)
           $q .= ' WHERE ' . $where;
       if ($order != null)
           $q .= ' ORDER BY ' . $order;
       if ($limit != null)
           $q .= ' LIMIT ' . $limit;
       $query = @mysql_query($q);
       $this -> querys++;
       if ($query) {
           $this -> numResults = mysql_num_rows($query);
           for ($i = 0; $i  numResults; $i++) {
               $r = mysql_fetch_array($query);
               $key = array_keys($r);
               for ($x = 0; $x                     if (!is_int($key[$x])) {
                       if (mysql_num_rows($query) > 1)
                           $this -> result[$i][$key[$x]] = $r[$key[$x]];
                       else if (mysql_num_rows($query)                             $this -> result = null;
                       else
                           $this -> result[$key[$x]] = $r[$key[$x]];
                   }
               }
           }
           return true;
       } else {
           $this->error = "select(): " . mysql_error();
           return false;
       }
   }

   /*
    * Insert values into the table
    * Required: table (the name of the table)
    *           values (the values to be inserted)
    * Optional: rows (if values don't match the number of rows)
    */
   public function insert($table, $values, $rows = null) {
       if ($this -> tableExists($table)) {
           $insert = 'INSERT INTO ' . $table;
           if ($rows != null) {
               $insert .= ' (' . $rows . ')';
           }

           for ($i = 0; $i                 if (is_string($values[$i]))
                   $values[$i] = '"' . $values[$i] . '"';
           }

           $values = implode(',', $values);

           $insert .= ' VALUES (' . $values . ')';

           $ins = @mysql_query($insert);
           $this -> querys++;
           if ($ins) {
               return true;
           } else {
               $this->error = "insert(): " . mysql_error();
               return false;
           }
       }
   }

   /*
    * Deletes table or records where condition is true
    * Required: table (the name of the table)
    * Optional: where (condition [column =  value])
    */
   public function delete($table, $where = null) {
       if ($this -> tableExists($table)) {
           if ($where == null) {
               $delete = 'DELETE ' . $table;
           } else {
               $delete = 'DELETE FROM ' . $table . ' WHERE ' . $where;
           }
           $del = @mysql_query($delete);
           $this -> querys++;

           if ($del) {
               return true;
           } else {
               $this->error = "delete(): " . mysql_error();
               return false;
           }
       } else {
           return false;
       }
   }

   /*
    * Updates the database with the values sent
    * Required: table (the name of the table to be updated
    *           rows (the rows/values in a key/value array
    *           where (the row/condition in an array (row,condition) )
    */
   public function update($table, $rows, $where) {
       if ($this -> tableExists($table)) {
           for ($i = 0; $i                 if ($i % 2 != 0) {
                   if (isset($where[$i])) {
                       if (($i + 1) != null)
                           $where[$i] = '"' . $where[$i] . '" AND ';
                       else
                           $where[$i] = '"' . $where[$i] . '"';
                   }
               }
           }
           $where = implode('', $where);

           $update = 'UPDATE ' . $table . ' SET ';
           $keys = array_keys($rows);
           for ($i = 0; $i                 if (is_string($rows[$keys[$i]])) {
                   $update .= $keys[$i] . '="' . $rows[$keys[$i]] . '"';
               } else {
                   $update .= $keys[$i] . '=' . $rows[$keys[$i]];
               }

               if ($i != count($rows) - 1) {
                   $update .= ',';
               }
           }
           $update .= ' WHERE ' . $where;
           $query = @mysql_query($update);
           $this -> querys++;
           if ($query) {
               return true;
           } else {
               $this->error = "update(): " . mysql_error();
               return false;
           }
       } else {
           return false;
       }
   }

   /*
    * Returns the result set in an array
    */
   public function getResult() {
       return $this -> result;
   }

   /*
    * Nulls the result set
    */
   public function nullResult() {
       $this -> result = array();
   }

}
?>

 

See ei ole tehtud minupoolt, küll aga olen ma seda muutnud ja stabiliseerinud ning muidugi lisanud funktsioone.

Ma kasutan seal oma config arrayd, te võite seda muidugi muuta. Samuti ei saada ma andmebaasi andmeid objekti ehitamisel vaid need võetakse configist, te võite muuta seda, et see tehtaks ära konstruktoris.


"What is happening to our young people? They disrespect their elders, they disobey their parents. They ignore the law. They riot in the streets inflamed with wild notions. Their morals are decaying. What is to become of them?"

Plato, 4th Century BC

Jaga seda postitust


Postituse link
Share on other sites

Kommentaari lisamiseks loo konto või logi sisse

Kommenteerimiseks peate olema liige

Loo konto

Liituge meie kommuuni uue kontoga. See on lihtne!

Loo uus konto

Logi sisse

On juba konto? Logi sisse siit.

Logi sisse nüüd

×
×
  • Loo uus...

Oluline informatsioon

Selle veebisaidi paremaks muutmiseks oleme teie seadmesse paigutanud küpsised . Võite kohandada oma küpsiste seadeid , vastasel juhul eeldame, et te olete küpsiste kasutamisega nõus kui jätkate veebisaidil sirvimist.. Palun lugege läbi Kasutustingimused ja Privaatsuspoliitika.