Posted By

jose_506 on 09/16/11


Tagged

mysql database class php


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

tux-world
ringo380
gecube


Class database mysql


 / Published in: PHP
 

  1. <?php
  2. class DatabaseMysql{
  3.  
  4. //Constants
  5. const host="localhost";
  6. const user="root";
  7. const password="";
  8. const test=true;
  9. const database="presupuestos";
  10. const email_admin="[email protected]";
  11.  
  12. //Variables privates
  13. private static $singleInstancia;
  14.  
  15.  
  16.  
  17. private $connection;
  18. private $selec_bbdd;
  19.  
  20.  
  21. //Constructor
  22.  
  23.  
  24. /*SINGLETON*/
  25. private function __construct()
  26. {
  27. //echo 'Estoy construido';
  28. //$this->database =isset($db)?$db:$this->err();
  29. }
  30.  
  31.  
  32. public static function getInstancia(){
  33. if(!self::$singleInstancia){
  34. self::$singleInstancia= new self();
  35. }
  36. return self::$singleInstancia;
  37. }
  38.  
  39. function Metodo(){
  40. var_dump(self::$singleInstancia);
  41. }
  42.  
  43. public function __clone()
  44. {
  45. trigger_error('No se permite la clonación.', E_USER_ERROR);
  46. }
  47.  
  48.  
  49. //Show erros the mysql
  50. function err(){
  51. if (self::test){
  52. echo "<b><font color='red'>ERROR:</b> --> </b>" .
  53. mysql_errno() . "</b> - <i>" . mysql_error() . "</i></font>";
  54. exit();
  55. }else{
  56. //echo "<b><font color='red'>Ha habido un error." . mysql_error() . "</font></b>";
  57. if (self::email_admin){
  58. //echo ", el administrador ha sido informado por email";
  59. mail(self::email_admin,
  60. "Error mysql en" . $_SERVER['PHP_SELF'],
  61. "Error-> " . mysql_error() .
  62. "\n en->" . $_SERVER['PHP_SELF'] . $_SERVER['QUERY_STRING'] .
  63. "\n a las-> " . date('H:i:s - D-d-m-Y'));
  64. }
  65. exit();
  66. }
  67. }
  68.  
  69. public function Connect(){
  70. $this->connection = @mysql_connect(self::host, self::user, self::password) or $this->err();
  71. $this->selec_bbdd = @mysql_select_db(self::database, $this->connection) or $this->err();
  72. }
  73.  
  74. // @todo: probar mysql_unbuffered_query() ya que no guarda en buffer y ahorra memoria
  75. // @todo: ver como hacer la liberacion de consultas grandes o especificar por parametro a la funcion si es consulta grande o no
  76. //registro seleccionados por el select mysql_num_rows()
  77. public function Queryarray($query){
  78. $result=mysql_query($query,$this->connection);
  79.  
  80. while($fila = mysql_fetch_array($result,MYSQL_NUM)){
  81. $arr_num[] = $fila;
  82. }
  83.  
  84. return($arr_num);
  85. }
  86.  
  87. //Query simple
  88. //
  89. public function Query($query){
  90. $result=mysql_query($query,$this->connection)or $this->err();
  91.  
  92. return $result;
  93. }
  94.  
  95. // @todo: Definir comentarios
  96. public function Queryassoc($query){
  97. $result=mysql_query($query,$this->connection);
  98.  
  99. while($fila = mysql_fetch_assoc($result)){
  100. $arr_asoc[] = $fila;
  101. }
  102.  
  103. return $arr_asoc;
  104. }
  105.  
  106. public function Queryassocwhere($table,$field,$value){
  107.  
  108. $query="SELECT * FROM $table WHERE $field=$value LIMIT 1";
  109.  
  110. $result=mysql_query($query,$this->connection);
  111.  
  112. while($fila = mysql_fetch_assoc($result)){
  113. $arr_asoc[] = $fila;
  114. }
  115.  
  116. return $arr_asoc;
  117. }
  118.  
  119. public function Queryrowwhere($table,$field,$value){
  120.  
  121. $query="SELECT * FROM $table WHERE $field='$value' LIMIT 1";
  122.  
  123. $result=mysql_query($query,$this->connection);
  124. $fila = mysql_fetch_row($result);
  125. return $fila;
  126. }
  127.  
  128. public function QueryObject($select,$table) {
  129. if(isset($table))
  130. {
  131. $fields=isset($select)?$select:'*';
  132. $query="SELECT $fields FROM $table";
  133. $result=mysql_query($query,$this->connection);
  134. return mysql_fetch_object($result);
  135. }
  136. }
  137.  
  138. // mysql_query_retorna true o false si es insert,delete,drrop y update y filas afectadas por la consulta mysql_affected_rows()
  139. public function Insertquery($query){
  140. $result=mysql_query($query)or $this->err();
  141.  
  142. return $result;
  143. }
  144.  
  145. public function Deletequery($field,$value,$table){
  146. return mysql_query("DELETE FROM `$this->database`.`$table` WHERE `$table`.`$field`=$value",$this->connection);
  147. }
  148.  
  149. public function Newidtable($field,$table){
  150. $query="SELECT MAX($field) AS id_max FROM $table";
  151. $result=mysql_query($query,$this->connection);
  152. $valuemax= mysql_fetch_row($result);
  153. return $valuemax[0];
  154. }
  155.  
  156. public function Close(){
  157. mysql_close($this->connection) or $this->err();
  158. }
  159.  
  160. public function Info(){
  161. $information="Version mysql: ". mysql_get_client_info()."<br/>";
  162. $information.=" Connection and hostname: ".mysql_get_host_info()."<br/>";
  163. $information.=" Protocol: ".mysql_get_proto_info()."<br/>";
  164. $information.=" Server version mysql: ".mysql_get_server_info()."<br/>";
  165. $information.=" Server version mysql: ".mysql_get_server_info()."<br/>";
  166. $information.=" ". mysql_stat($this->connection)."<br/>";
  167. return $information;
  168. }
  169.  
  170. public function ListTable($table,$type=NULL){
  171. if(isset($type)){
  172. return $type=='array'?$this->Queryarray("SELECT * FROM $table"):$this->Queryassoc("SELECT * FROM $table");
  173. }else{
  174. return $this->Queryarray("SELECT * FROM $table");
  175. }
  176. }
  177.  
  178. public function ListTableJoin($table1,$table2,$field1,$field2,$select){
  179. try{
  180. $fields = isset($select)?$select:'*';
  181. return $this->Queryassoc("SELECT $fields FROM $table1 AS t1 INNER JOIN $table2 AS t2 ON t1.$field1 = t2.$field2");
  182.  
  183. }catch(Exception $e){
  184. echo 'Excepción capturada: ', $e->getMessage(), "\n";
  185. }
  186.  
  187. }
  188.  
  189. public function ExistRecords($table,$where=NULL){
  190. if(isset($where)){
  191. $result = mysql_query("SELECT * FROM $table WHERE $where", $this->connection) or $this->err();
  192. }else{
  193. $result = mysql_query("SELECT * FROM $table", $this->connection) or $this->err();
  194. }
  195. return mysql_num_rows($result);
  196. // return $this->connection;
  197. }
  198.  
  199.  
  200.  
  201. //@todo: crear funcion de chequeo de coenxion con el servidor o de ping
  202.  
  203.  
  204.  
  205. public function get($name){
  206. return $this->$name;
  207. }
  208. }
  209. ?>

Report this snippet  

You need to login to post a comment.