Posted By

Knarf on 11/15/09


Tagged

mysql class php


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

jlan


Class MySQL


 / Published in: PHP
 

A class for simply connect and do a query on a database

  1. /*
  2. * A class for simply connect and do a query on a database
  3. *
  4. */
  5.  
  6. Class MySQL
  7. {
  8. private static $connection;
  9.  
  10. private static $havequeryed;
  11. private static $haveconnected;
  12.  
  13. private static $config;
  14. private static $database;
  15.  
  16. public function __construct($config, $database)
  17. {
  18. if (isset($this->haveconnected)) {
  19. if ($this->haveconnected == True) { self::close(); }
  20. }
  21.  
  22. $this->config = $config;
  23. $this->database = $database;
  24. $this->havequeryed = False;
  25. $this->haveconnected = True;
  26.  
  27. $this->connection = mysql_connect($config["host"], $config["user"], $config["pass"]);
  28. mysql_select_db($database, $this->connection);
  29. }
  30.  
  31. public function query($query)
  32. {
  33. if ($this->havequeryed == True) {
  34. self::__construct($this->config, $this->database);
  35. }
  36.  
  37. $this->havequeryed = True;
  38. return mysql_query($query, $this->connection);
  39. }
  40.  
  41. public function list_dbs($result = array())
  42. {
  43. $list = mysql_list_dbs( $this->connection );
  44. $output = $list;
  45.  
  46. if( isset($result["array"]) && $result["array"] == True)
  47. {
  48. $output = Array();
  49.  
  50. While( $row = mysql_fetch_assoc( $list ) ):
  51. array_push($output, $row['Database']);
  52. endwhile;
  53. }
  54.  
  55. return $output;
  56. }
  57.  
  58. function __destruct() {
  59. if(is_object($this->connection)) {
  60. mysql_close( $this->connection );
  61. }
  62. }
  63.  
  64. public function close()
  65. {
  66. self::__destruct();
  67. }
  68. }

Report this snippet  

You need to login to post a comment.