Posted By

rutti on 11/06/12


Tagged

MySQLClassPHP


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

rutti
leotaillard
eorhed


PHP Simple Class For MySQL


 / Published in: PHP
 

a brief Class of frequently used MySQL functions.

  1. <?php
  2. // Define for MySQL //////////////////////////////////////////////
  3. define('LOCALHOST','localhost');
  4. define('DB_USER','****');
  5. define('DB_PASS','****');
  6. define('DB_NAME','****');
  7.  
  8. ////////////////////////////////////////////////////////
  9. // Class
  10. ////////////////////////////////////////////////////////
  11. class DB {
  12. var $host; // host
  13. var $user; // user
  14. var $pass; // pass
  15. var $db; // MySQL_LinkID
  16. var $database; // DB name
  17.  
  18. ///////////////////////////////////////////////////////
  19. // construct
  20. ///////////////////////////////////////////////////////
  21. function DB() {
  22. $this->host = LOCALHOST;
  23. $this->user = DB_USER;
  24. $this->pass = DB_PASS;
  25. $this->database = DB_NAME;
  26. }
  27.  
  28. ///////////////////////////////////////////////////////
  29. // Connection
  30. ///////////////////////////////////////////////////////
  31. function con() {
  32. $this->db = mysql_connect($this->host,$this->user,$this->pass);
  33. if ( !$this->db ) {
  34. die ('mysql connect error : ' . mysql_error());
  35. }
  36.  
  37. $db_selected = mysql_select_db($this->database,$this->db);
  38. if (!$db_selected) {
  39. die ('mysql select db error : ' . mysql_error());
  40. }
  41.  
  42. }
  43.  
  44. ///////////////////////////////////////////////////////
  45. // Close
  46. ///////////////////////////////////////////////////////
  47. function close() {
  48. mysql_close($this->db);
  49. }
  50.  
  51. ///////////////////////////////////////////////////////
  52. // Query
  53. ///////////////////////////////////////////////////////
  54. function query($query) {
  55. $res = mysql_query($query,$this->db) or die("error : " . mysql_error($this->db)."<br />".$query);
  56. return $res;
  57. }
  58. }
  59.  
  60. ////////////////////////////////////////////////////////
  61. // ShortCut Function
  62. ////////////////////////////////////////////////////////
  63.  
  64. // mysql_num_rows
  65. function mnr($res) {
  66. $num = mysql_num_rows($res);
  67. return $num;
  68. }
  69.  
  70. // mysql_fetch_assoc
  71. function mfa($res) {
  72. $rec = mysql_fetch_assoc($res);
  73. return $rec;
  74. }
  75.  
  76. // mysql_fetch_array
  77. function mfar($res) {
  78. $rec = mysql_fetch_array($res);
  79. return $rec;
  80. }
  81.  
  82. ?>

Report this snippet  

You need to login to post a comment.