host="localhost";//$Puser $this->usuario="user_bd_maquinas";// $this->password="mGkk899!";//$Ppassword $this->str_aux_bd=$database; $function = ($persistente) ? 'mysql_pconnect' : 'mysql_connect'; if (!$this->conexion = $function($this->host, $this->usuario, $this->password)) return FALSE; if (!mysql_select_db($this->str_aux_bd, $this->conexion)) { trigger_error(mysql_error($this->conexion), E_USER_WARNING); return FALSE; } return TRUE; } function mysqlclose (){ mysql_close($this->conexion); } function query($query, $key = 0, $debug = FALSE) { $this->last_query = $query; if ($debug) echo ($this->output_html) ? "".htmlspecialchars($query)."
\n" : "$query\n"; /*if ($_SERVER["REMOTE_ADDR"] == '87.219.234.100'){ echo $query; }*/ //echo $query; if (!$this->resultados[$key] = mysql_query($query, $this->conexion)) { trigger_error(mysql_error($this->conexion), E_USER_WARNING); return FALSE; } return TRUE; } function begin() { return $this->query('BEGIN', uniqid(0)); } function rollback() { return $this->query('ROLLBACK', uniqid(0)); } function commit() { return $this->query('COMMIT', uniqid(0)); } function cell($query) { $key = uniqid(0); $this->query($query, $key); if ($this->row_count()) return mysql_result($this->resultados[$key], 0); } function row($key = 0) { return mysql_fetch_object($this->resultados[$key]); } function row_id($key = 0) {//retorna els camps amb id return mysql_fetch_row($this->resultados[$key]); } function all_rows($query) { $key = uniqid(0); $this->query($query, $key); while ($row = $this->row($key)) $rows[] = $row; return $rows; } function row_count() { $command = strtoupper(substr($this->last_query, 0, 6)); return ($command == 'INSERT' || $command == 'UPDATE' || $command == 'DELETE') ? mysql_affected_rows($this->conexion) : mysql_num_rows(max($this->resultados)); } function quote($string) { return (get_magic_quotes_gpc()) ? $string : addslashes($string); } function limit($rows, $offset = 0) { return "LIMIT $offset, $rows"; } function last_id($table, $serial_column = 'id') { return ($contents = mysql_insert_id($this->conexion)) ? $contents : FALSE; } function serial() { return 'INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY'; } function seconds($column) { return "UNIX_TIMESTAMP($column)"; } function insert($P_valor,$P_tabla){ while(list($k,$v)=each($P_valor)){ $columnas[]=$k; $valor[]="'".$v."'"; } $columnas=implode(' , ',$columnas); $valor=implode(' , ',$valor); $sql="INSERT INTO ".$P_tabla." ( ".$columnas." ) VALUES ( ".$valor." );"; return $this->query($sql); } function del($P_table,$P_where){ $sql="DELETE FROM ".$P_table."".$this->crear_where($P_where).";"; $this->query($sql); $this->optimizar($P_table); } function crear_where($P_where){ if(is_array($P_where)){ $this->str_aux_bd=" WHERE "; while(list($camp,$val)=each($P_where)){ $condicio[]=$camp."='".$val."'"; } $this->str_aux_bd.=implode(' AND ',$condicio); }else{ $this->str_aux_bd=""; } return $this->str_aux_bd; } function update($P_valor,$P_tabla,$P_where){ while(list($k,$v)=each($P_valor)){ $columnas_valor[]=$k."='".$v."'"; } $columnas_valor=implode(' , ',$columnas_valor); $sql="UPDATE ".$P_tabla." SET ".$columnas_valor." ".$this->crear_where($P_where).";"; $this->query($sql); } function reparar($P_table){ $sql="REPAIR TABLE ".$P_table.";"; return $this->query($sql); } function optimizar($P_table){ $sql="OPTIMIZE TABLE ".$P_table.";"; return $this->query($sql); } //function per extrere querys petits function Set_query($P_sql){ $key = uniqid(0); $aux=NULL; $this->query($P_sql,$key); if($this->row_count($key)){ while($r=$this->row($key)){ //colquem tots els resultats dins d'un array multidimensional //$aux[idautomatic]=array(nomdel camp, valor); $aux[]=get_object_vars($r); } } return $aux; } function Set_query_simple($P_sql){ $key = uniqid(0); $aux=NULL; $this->query($P_sql,$key); if($this->row_count($key)){ while($r=$this->row_id($key)){ //colquem tots els resultats dins d'un array multidimensional //$aux[iddb]= valor; $aux[$r[0]]=stripslashes($r[1]); } } return $aux; } function Set_sql($P_taula,$P_simple_sql=1,$P_were="",$P_oder="",$P_camps="*"){ $sql="SELECT ". $P_camps." FROM ".$P_taula." ".$this->crear_where($P_were)." ".$this->Set_order($P_oder).";"; if($P_simple_sql==1){//retorna array amb valor id/nom ideal per selects return $this->Set_query_simple($sql); }else{ return $this->Set_query($sql); } } function Set_order($P_order){ $aux=""; if(is_array($P_order)){ $__contador=0; foreach($P_order as $k_order=>$v_order){ $_DESC_ASC_=$v_order? $v_order:"ASC"; $__order__[$__contador]=$k_order." ".$_DESC_ASC_; $__contador++; } $aux="ORDER BY "; $aux.=implode(',',$__order__); } return $aux; } function Set_Where($P_were){ $aux=NULL; if($P_were || is_array($P_were)){ $aux=$this->crear_where($P_where); } return $aux; } function resetRecordSet ($key = 0){ mysql_data_seek($this->resultados[$key], 0); } } ?>