/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php class DatabaseMysql{ //Constants const host="localhost"; const user="root"; const password=""; const test=true; const database="presupuestos"; //Variables privates private static $singleInstancia; private $connection; private $selec_bbdd; //Constructor /*SINGLETON*/ private function __construct() { //echo 'Estoy construido'; //$this->database =isset($db)?$db:$this->err(); } public static function getInstancia(){ if(!self::$singleInstancia){ self::$singleInstancia= new self(); } return self::$singleInstancia; } function Metodo(){ } public function __clone() { } //Show erros the mysql function err(){ if (self::test){ echo "<b><font color='red'>ERROR:</b> --> </b>" . }else{ //echo "<b><font color='red'>Ha habido un error." . mysql_error() . "</font></b>"; if (self::email_admin){ //echo ", el administrador ha sido informado por email"; "Error mysql en" . $_SERVER['PHP_SELF'], "\n en->" . $_SERVER['PHP_SELF'] . $_SERVER['QUERY_STRING'] . } } } public function Connect(){ } // @todo: probar mysql_unbuffered_query() ya que no guarda en buffer y ahorra memoria // @todo: ver como hacer la liberacion de consultas grandes o especificar por parametro a la funcion si es consulta grande o no //registro seleccionados por el select mysql_num_rows() public function Queryarray($query){ $arr_num[] = $fila; } return($arr_num); } //Query simple // public function Query($query){ return $result; } // @todo: Definir comentarios public function Queryassoc($query){ $arr_asoc[] = $fila; } return $arr_asoc; } public function Queryassocwhere($table,$field,$value){ $query="SELECT * FROM $table WHERE $field=$value LIMIT 1"; $arr_asoc[] = $fila; } return $arr_asoc; } public function Queryrowwhere($table,$field,$value){ $query="SELECT * FROM $table WHERE $field='$value' LIMIT 1"; return $fila; } public function QueryObject($select,$table) { { $query="SELECT $fields FROM $table"; } } // mysql_query_retorna true o false si es insert,delete,drrop y update y filas afectadas por la consulta mysql_affected_rows() public function Insertquery($query){ return $result; } public function Deletequery($field,$value,$table){ return mysql_query("DELETE FROM `$this->database`.`$table` WHERE `$table`.`$field`=$value",$this->connection); } public function Newidtable($field,$table){ $query="SELECT MAX($field) AS id_max FROM $table"; return $valuemax[0]; } public function Close(){ } public function Info(){ return $information; } public function ListTable($table,$type=NULL){ return $type=='array'?$this->Queryarray("SELECT * FROM $table"):$this->Queryassoc("SELECT * FROM $table"); }else{ return $this->Queryarray("SELECT * FROM $table"); } } public function ListTableJoin($table1,$table2,$field1,$field2,$select){ try{ return $this->Queryassoc("SELECT $fields FROM $table1 AS t1 INNER JOIN $table2 AS t2 ON t1.$field1 = t2.$field2"); }catch(Exception $e){ echo 'Excepción capturada: ', $e->getMessage(), "\n"; } } public function ExistRecords($table,$where=NULL){ }else{ } // return $this->connection; } //@todo: crear funcion de chequeo de coenxion con el servidor o de ping public function get($name){ return $this->$name; } } ?>