Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class mysql{
- var $dbhost=DB_HOST;
- var $dbuser=DB_USER ;
- var $dbpass=DB_PASS ;
- var $dbase=DB_NAME;
- var $link;
- var $result;
- var $nr_rows;
- var $sql;
- var $affected;
- var $count;
- function debug(){
- echo $this->sql;
- echo mysql_errno($this->link)." - ".mysql_error($this->link);
- die();
- }
- function connect(){
- if (empty($this->link)){
- $this->link = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass) or die("Unable to connect") ;
- mysql_select_db($this->dbase,$this->link) or die ("Unable to select DB");
- }
- }
- function disconect(){
- mysql_close($this->link);
- }
- function query($query = "" ){
- $this->count++;
- if (!$this->link) $this->connect();
- if (!empty($query)) $this->sql = $query;
- if(!empty($this->sql)){
- if (!($this->result = @mysql_query($this->sql))){
- $this->debug();
- } else {
- //$this->nr_rows =mysql_num_rows($this->result);
- }
- //return $this->result;
- }
- }
- function insert($table,$ArrayFieldsAndValues){
- $fields = array();
- $values = array();
- $this->sql = "INSERT INTO $table (";
- foreach ($ArrayFieldsAndValues as $key=>$val){
- $fields[] = $key;
- $values[] = $val;
- }
- foreach ($fields as $field)
- $this->sql .=$field ." ,";
- $this->sql{strlen($this->sql)-1} = ")";
- $this->sql.=" VALUES (";
- foreach ($values as $val)
- $this->sql .="'".$val ."',";
- $this->sql{strlen($this->sql)-1} = ")";
- $this->query();
- }
- function __getAll(){
- $this->query();
- $r = array();
- while ($rb = mysql_fetch_assoc($this->result)){
- $r[] = $rb;
- };
- return $r;
- }
- function getAll($sql = ""){
- if(!empty($sql)){
- $this->sql = $sql;
- $this->query();
- }
- return $this->__getAll();
- }
- function getRow($sql = ""){
- if(!empty($sql)){
- $this->sql = $sql;
- $this->query();
- }
- $r = mysql_fetch_assoc($this->result);
- return $r;
- }
- function getColumns($table){
- $this->query("DESCRIBE `$table`");
- $columns=array();
- foreach ($this->getAll() as $v) {
- $columns[]=$v['Field'];
- }
- return $columns;
- }
- function update($table,$Array,$condition){
- $compose = array();
- $Array=$this->escape_array($Array);
- if (is_array($this->getColumns($table))){
- foreach ($this->getColumns($table) as $key){
- $val = $Array[$key];
- if (!empty($val)) {
- $compose[] = "`$key`" ."= '".$val."'";
- }
- }
- }
- $this->sql = "UPDATE `$table` SET " . implode(", ",$compose)." WHERE $condition";
- $this->query($this->sql);
- }
- function escape($string){
- $this->connect();
- if(!is_array($string))
- return mysql_real_escape_string($string,$this->link);
- }
- function escape_array($array) {
- foreach ($array as $k=>$v) {
- $array[$k] = escape_data($v);
- }
- return $array;
- }
- function last_id(){
- return mysql_insert_id($this->link);
- }
- function num_rows($sql=''){
- if (!$this->link) $this->connect();
- if (!empty($sql)) $this->sql = $sql;
- $this->query();
- return mysql_num_rows($this->result);
- }
- function disconnect(){
- if($this->link) mysql_close($this->link);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement