host = DBHOST; $this->user = DBUSER; $this->pass = DBPASS; $this->db = DBNAME; $this->connect($host, $user, $pass, $db, false); } function connect($host='', $user='', $pass='', $db='', $persistant=false) { if (!empty($host)) $this->host = $host; if (!empty($user)) $this->user = $user; if (!empty($pass)) $this->pass = $pass; if ($persistant) $this->link = @mysql_pconnect($this->host, $this->user, $this->pass) or $this->connectionError(mysql_error()); else $this->link = @mysql_connect($this->host, $this->user, $this->pass) or $this->connectionError(mysql_error()); if (!$this->link) { $this->connectionError(mysql_error()); return false; } // Select the database if (!$this->db_select($db)) return false; return true; // success } function db_select($db='') { if (!empty($db)) $this->db = $db; if (!mysql_select_db($this->db)) { $this->connectionError(mysql_error()); return false; } return true; } function connectionError($err) { if(!DEBUGMODE) { $errMsg="
ERROR!!".$err."
"; } else { $errMsg="
ERROR!! Can not Connect with DB.Please Try Latter
"; } echo "
".$errMsg; exit; } } ?>
ERROR!!
QUERY EMPTY
"; echo "
".$errMsg; } $this->sqlQuery=$sql; } function getQuery() { if($this->sqlQuery=="") { $errMsg="
ERROR!!
QUERY EMPTY
"; echo "
".$errMsg; } return $this->sqlQuery; } function data_prepare($string,$qto=1) { if(is_string($string)) { $string=htmlspecialchars(addslashes(stripslashes(trim($string)))); if(!$qto || $this->isMysqlKeyword($string)) { return $string; } $string="'$string'"; return $string; } else if(is_numeric($string)) { return $string; } else { if(!$qto) { return $string; } $string="''"; return $string; } } function getFields($table,$datagiven) { $fields = @mysql_list_fields(DBNAME, $table) or $this->mysqlError($sql); $column_num = @mysql_num_fields($fields) or $this->mysqlError($sql); for ($i = 0; $i < $column_num; $i++) { $field_name=@mysql_field_name($fields, $i) or $this->mysqlError($sql); foreach ($datagiven as $key=>$value) { if($key==$field_name) $data[$key]=$value; } } return $data; } function getFieldsOnly($table,$toreturn="",$alise="",$extra="") { $fields = @mysql_list_fields(DBNAME, $table) or $this->mysqlError($sql); $column_num = @mysql_num_fields($fields) or $this->mysqlError($sql); if(empty($toreturn)) { $toreturn=array(); } for ($i = 0; $i < $column_num; $i++) { $field_name=@mysql_field_name($fields, $i) or $this->mysqlError($sql); if(!in_array($field_name,$toreturn)) { $field_name_al=$alise.$field_name.$extra; $field_name_arr[]=$field_name_al; } } return $field_name_arr; } //INSERT FUNCTION function insertData($table,$datagiven) { $data=$this->getFields($table,$datagiven); $cols = '('; $values = '('; foreach ($data as $key=>$value) { $value=$this->data_prepare($value); $cols .= "$key,"; $values .= "$value,"; } $cols = rtrim($cols, ',').')'; $values = rtrim($values, ',').')'; $sql = "INSERT INTO ".$table." ".$cols." VALUES ".$values; $this->setQuery($sql); $result = @mysql_query($sql); if (!$result) $this->mysqlError($sql); $id = mysql_insert_id(); if ($id == 0) return true; else return $id; // $sql } //UPDATE FUNCTION function updateData($table, $datagiven, $condition) { $data=$this->getFields($table,$datagiven); $sql = "UPDATE ".$table." SET"; foreach ($data as $key=>$value) { $value=$this->data_prepare($value); $sql .= " $key="; $sql .= "$value,"; } $sql = rtrim($sql, ','); if (!empty($condition)) $sql .= " WHERE ".$condition; $this->setQuery($sql); $result = @mysql_query($sql); if (!$result) $this->mysqlError($sql); $rows = mysql_affected_rows(); if ($rows == 0) return true; else return $rows; } //DELETE FUNCTION function deleteData($table, $condition) { $sql = "Delete from ".$table; if (!empty($condition)) $sql .= " WHERE ".$condition; $this->setQuery($sql); $result = @mysql_query($sql); if (!$result) $this->mysqlError($sql); $rows = mysql_affected_rows(); if ($rows == 0) return true; else return $rows; } //SELECT FUNCTION function selectData($table,$parameter="",$condition="",$record="",$orderby="",$groupby="",$lmt="") { if(empty($parameter)) { $parameter="*"; } $sql="select ".$parameter." from ".$table; if(!empty($condition)) { $sql.=" where ".$condition; } if(!empty($groupby)) { $sql.=" group by ".$groupby; } if(!empty($orderby)) { $sql.=" order by ".$orderby; } if(!empty($lmt)) { $sql.=" limit ".$lmt; } $this->setQuery($sql); $res = @mysql_query($sql) or $this->mysqlError($sql); if($record==1) { $numres=@mysql_num_rows($res); $this->numRows=$numres; if($numres==0) { $res=0; } else { $res=@mysql_fetch_array($res); } } if($record==2) { $res=$sql; } return $res; } function qUpdate($table,$parameter,$condition="") { if(empty($condition)) $condition=1; $sql="update ".$table." set ".$parameter." where ".$condition; $this->setQuery($sql); $res = @mysql_query($sql) or $this->mysqlError($sql); return mysql_affected_rows(); } function qDelete($table,$condition) { if(!empty($condition)) { $sql="delete from ".$table." where ".$condition; $this->setQuery($sql); $res = @mysql_query($sql) or $this->mysqlError($sql); return mysql_affected_rows(); } else return 0; } //Counting Number of elements function countRows($sql) { $result= @mysql_query($sql) or $this->mysqlError($sql); $num_rows = @mysql_num_rows($result); return $num_rows ; } //MYSQL Error function mysqlError($sql="") { if(!DEBUGMODE) { $qry=""; if(!empty($sql)) $qry="
QUERY
".$sql; $errorNo=mysql_errno(); $errMsg=mysql_error(); $errMsg="
ERROR!!".$errorNo."
".$errMsg.$qry."
"; } else { $errMsg="
ERROR!! Can not complete the operation.Please Try Latter.
"; } echo "
".$errMsg; } function setMysqlKeyword($str="",$sep=",") { $this->mysqlKeyword=explode($sep,$str); } function isMysqlKeyword($string) { if(in_array($string,$this->mysqlKeyword)) { return true; } return false; } } ?>