Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. <?php
  2.  
  3. class mysql extends phpSite
  4. {
  5. var $DBHost,$DBUser,$DBPass,$DBPort,$DBConn,$DBName,$DBTables;
  6.  
  7. //constructor for mysql class, also the mysql connector
  8. function mysql ($DBHost,$DBUser,$DBPass,$DBName,$DBPort=3306)
  9. {
  10. try
  11. {
  12. $this->DBConn = new mysqli($DBHost,$DBUser,$DBPass,$DBName,$DBPort);
  13. if ($this->DBConn->connect_error)
  14. throw new Exception("Error occurred (".$this->DBConn->connect_errno."): ".$this->DBConn->connect_error,100);
  15. }
  16. catch (Exception $e)
  17. {
  18. $this->ErrorHandler($e);
  19. }
  20. }
  21.  
  22.  
  23. //Function to return query as an object or array
  24. public function query($sql,$returnType = 1)
  25. {
  26. $query = $this->DBConn->query($sql);
  27. $num = @$query->num_rows;
  28.  
  29. if ($num>0)
  30. {
  31. $record = array();
  32. if ($returnType == 1)
  33. {
  34. while ($rs=$query->fetch_object())
  35. {
  36. $record[] = $rs;
  37. }
  38. $record = (object)$record;
  39. }
  40. else
  41. {
  42. while ($rs=$query->fetch_array())
  43. {
  44. $record[] = $rs;
  45. }
  46. }
  47. return $record;
  48. }
  49. else
  50. {
  51. return array();
  52. }
  53. }
  54.  
  55.  
  56. //Function to close mysql connection. Will call destructor, since class can't be used without mysql-connection..
  57. function close()
  58. {
  59. unset($this);
  60. }
  61.  
  62.  
  63. //Destructor - called when class is destroyed.
  64. function __destruct()
  65. {
  66. try
  67. {
  68. if ($this->DBConn->close() === false)
  69. throw new Exception("Could not close connection to database server.
  70. Does connection exist?",101);
  71. }
  72. catch (Exception $e)
  73. {
  74. $this->ErrorHandler($e);
  75. }
  76. }
  77.  
  78.  
  79. }
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement