Location: PHPKode > projects > KvCMS Content Management System > kvframework/kvframework/dbhandler/dbtypes/kvframework_dbtype_mysql.class.php
<?php

// +----------------------------------------------------------------------
// | PHP Source
// +----------------------------------------------------------------------
// | Copyright (C) 2005 by Gregory McWhirter <hide@address.com>
// +----------------------------------------------------------------------
// |
// | Copyright: See COPYING file that comes with this distribution
// +----------------------------------------------------------------------
//

/*
 * $q = self::$DB->find_query($tables)
 * $q->conditions = "";
 * $results = self::$DB->process($q);
 */

class kvframework_dbtype_mysql extends kvframework_dbtype {
  private $dbc;
  private $tables_fields = array();

  public function __construct(array $dbinfo){
    //$this->dbc = @mysql_connect($dbinfo['dbhost'], $dbinfo['dbuser'], $dbinfo['dbpass']);
    //@mysql_select_db($dbinfo['dbname'], $this->dbc);
    $this->dbc = new mysqli($dbinfo['dbhost'], $dbinfo['dbuser'], $dbinfo['dbpass'], $dbinfo['dbname']);
    parent::__construct();
  }

  public function process(kvframework_struct_query $query){
    switch($query->action){
      case "find":
        $sql = "SELECT ".((count($query->fields) > 0) ? implode(", ", $query->fields) : "*")." FROM ".implode(", ", $query->tables).(($query->conditions != "") ? " WHERE ".$query->conditions : "").(($query->order != "") ? " ORDER BY ". $query->order : "").(($query->limit != "") ? " LIMIT ".$query->limit : "");
        $q = $this->query($sql);
        if($query->limit == "1"){
          $ret = array();
          while($row = $this->fetch($q)){
            $ret[] = $row;
          }
        } else {
          $ret = $this->fetch($q);
        }
        return $ret;
        break;
      case "delete":
        $sql = "DELETE FROM ".implode(", ",$query->tables).(($query->conditions != "") ? " WHERE ".$query->conditions : "").(($query->limit != "") ? " LIMIT ".$query->limit : "");
        return $this->query($sql);
        //return(($q) ? true : false );
        break;
      case "insert":
        $ks = "";
        $vs = "";
        foreach($query->fields as $k => $v){
          $ks .= "".$k.", ";
          $vs .= "'".$this->escape($v)."', ";
        }
        $ks = substr($ks, 0, -2);
        $vs = substr($vs, 0, -2);
        $sql = "INSERT INTO ".implode(", ",$query->tables)." (".$ks.") VALUES (".$vs.")";
        $q = $this->query($sql);
        if($this->affected() > 0){return $this->inserted();}
        else{return false;}
        break;
      case "update":
        $flds = "";
        foreach($query->fields as $k => $v){
          $flds .= $k." = '".$this->escape($v)."', ";
        }
        $flds = substr($flds, 0, -2);
        $sql = "UPDATE ".implode(", ",$query->tables)." SET ".$flds." WHERE ".$query->conditions.(($query->limit != "") ? " LIMIT ".$query->limit : "");
        $q = $this->query($sql);
        if($q){return true;}
        else{return false;}
        break;
    }
  }

  public function query($sql){
    kvframework_log::write_log($sql, KVF_LOG_LINFO, array("sql"), "SQL");
    $query = $this->dbc->query($sql);
    //$query = @mysql_query($sql, $this->dbc);
    if(!$query){throw new kvframework_db_exception($this->error("text"), $this->error("number"));}
    return ($query instanceOf mysqli_result) ? new kvframework_db_query($query) : $query;
  }

  public function fetch(kvframework_db_query $qid){
    //$row = @mysql_fetch_object($qid);
    $row = (!$qid->is_closed()) ? $qid->fetch_object("kvframework_db_object") : null;
    if(!$qid->is_closed() && (!$row || $this->rows($qid) == 1)){$qid->close();}
    return $row;
  }

  public function fetch_array(kvframework_db_query $qid){
    //$row = @mysql_fetch_assoc($qid);
    $row = (!$qid->is_closed()) ? $qid->fetch_assoc() : null;
    if(!$qid->is_closed() && (!$row || $this->rows($qid) == 1)){$qid->close();}
    return $row;
  }

  public function rows(kvframework_db_query $qid){
    //$number = @mysql_num_rows($qid);
    $number = $qid->num_rows;
    return $number;
  }

  public function error($ret){
    //if($ret == "text"){ return @mysql_error($this->dbc);}
    //elseif($ret == "number"){ return @mysql_errno($this->dbc);}
    //elseif($ret == "array"){ return array(@mysql_error($this->dbc), @mysql_errno($this->dbc));}
    if($ret == "text"){ return $this->dbc->error;}
    elseif($ret == "number"){ return $this->dbc->errno;}
    elseif($ret == "array"){ return array($this->dbc->error, $this->dbc->errno);}
    else{ return "";}
  }

  public function affected(){
    //$number = @mysql_affected_rows($this->dbc);
    $number = $this->dbc->affected_rows;
    return $number;
  }

  public function inserted(){
    //$number = mysql_insert_id($this->dbc);
    $number = $this->dbc->insert_id;
    return $number;
  }

  public function escape($val){
    //return @mysql_real_escape_string($val, $this->dbc);
    return @mysqli_real_escape_string($this->dbc, $val);
  }
}
?>
Return current item: KvCMS Content Management System