<?php
class kdb_conn{
public $db, $user, $pass, $conn, $last_id, $recordsLength;
function __construct($host, $user, $pass, $db){
$this->host = $host;
$this->db = $db;
$this->user = $user;
$this->pass = $pass;
}
function setConnection($conn){
$this->conn = $conn;
}
function &getConnection(){
$conn;
if(!isset($this->conn)){
$conn = mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->db, $conn);
}
else{
$conn = $this->conn;
}
$this->activeConn = $conn;
return $conn;
}
function insert($table, $fields, $options = array()){
$op = array_merge(array('escape'=> true, 'dynamic'=> array(), 'auto_update'=> false), $options);
$key = null;
if($op['auto_update'] ){
$key = "id";
if( $op['auto_update']!==true){
$key = $op['auto_update'];
}
}
if($op['auto_update'] && isset($fields[$key])){
$where = array($key=> $fields[$key]);
unset($fields[$key]);
return $this->update($table, $fields, $where);
}
$f = $v = "";
foreach($fields as $i=>$_v){
//print_r($op['dynamic']);
$is_dyn = $_v =='now()' ||
(is_array( $op['dynamic']) && in_array( $i, $op['dynamic'])) ||
(!is_array( $op['dynamic']) && $i == $op['dynamic']);
if(!$is_dyn && $op['escape'] ){
$_v = mysql_escape_string($_v);
}
if($f!=""){
$f.=", ";
$v.=", ";
}
$f .= "`$i`";
if($is_dyn){
$v .= "$_v";
} else{
$v .= "'$_v'";
}
}
$q = "INSERT INTO $table($f) VALUES($v)";
//echo $q; die;
$rs = $this->query($q, $options);
$this->last_id = $this->get_var("SELECT LAST_INSERT_ID()");
return $this->last_id;
}
function update($table, $fields, $where, $options=array()){
$op = array_merge(array('escape'=> true, 'dynamic'=> array()), $options);
$f = $w = "";
foreach($fields as $i=>$_f){
$is_dyn = $_f =='now()' ||
(is_array( $op['dynamic']) && in_array( $i, $op['dynamic'])) ||
(!is_array( $op['dynamic']) && $i == $op['dynamic']);
if(!$is_dyn || $op['escape'] ){ //echo " $_f, ";
$_f = mysql_escape_string($_f);
}
$f .= $f==""?"":", ";
if(!$is_dyn){
$f .= "`$i` = '$_f'";
} else{
$f .= "`$i` = $_f";
}
}
foreach($where as $i=>$_w){
$is_dyn = $_w =='now()' ||
(is_array( $op['dynamic']) && in_array( $i, $op['dynamic'])) ||
(!is_array( $op['dynamic']) && $i == $op['dynamic']);
if(!$is_dyn || $op['escape'] ){
$_w = mysql_escape_string($_w);
}
$w .= $w==""?"":" AND ";
if(!$is_dyn){
$w .= "`$i` = '$_w'";
} else{
$w .= "`$i` = $_w";
}
}
if(!empty($w)){
$w = " WHERE $w";
}
$qry = "UPDATE $table SET $f $w";
// echo $qry; die;
return $this->query($qry, $options);
}
function query($sql, $options=array()){
$arr=false;
if(isset($options['numericIndex']) && $options['numericIndex']){
$arr = true;
}
$this->getConnection();
$results = Array();
if(isset($options['debug']) && $options['debug']==true){
echo "<p style='color:red;'> $sql </p><br/>";
}
$rs = mysql_query($sql, $this->activeConn);
//var_dump($rs); //echo "<-- in qry "; var_dump($this->activeConn); var_dump($rs); echo $sql.'<br>'; die;
if($rs===true || $rs===false){
return $rs;
}
if($arr==false){
while($ar = mysql_fetch_assoc($rs)){
//print_r($ar); die;
$results[] = $ar;
}
}
else{
$results[] = mysql_fetch_array($rs);
}
//print_r($results); die;
return $results;
}
function get_list($query, $fields=Array('id', 'name'), $options=array()){
$rs = $this->query($query, $options);
$rt = Array();
foreach($rs as $i=>$v){
$rt[$v[$fields[0]]] = $v[$fields[1]];
}
return $rt;
}
function get_var($query, $options=array()){
$options['numericIndex'] = true;
$rs = $this->query($query, $options);
return count($rs)>0 && count($rs[0])>0? $rs[0][0]: null;
}
function get_row($query, $options = array()){
$rs = $this->query($query, $options);
$rs = count($rs)==0 ? $rs:$rs[0];
return $rs;
}
function close(){
mysql_close($this->activeConn);
}
}
?> |