api.dbconnect.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. error_reporting(E_ALL ^ E_DEPRECATED); //hide some deprecated warnings on 5.6 :(
  3. /**
  4. * Yet another manual database connection class
  5. */
  6. class DbConnect {
  7. var $host = '';
  8. var $dbport = 3306;
  9. var $user = '';
  10. var $password = '';
  11. var $database = '';
  12. var $persistent = false;
  13. var $conn = NULL;
  14. var $result = false;
  15. var $error_reporting = false;
  16. public function __construct($host, $user, $password, $database, $error_reporting = true, $persistent = false, $port = 3306) {
  17. $this->dbport = (empty($port)) ? 3306 : $port;
  18. //we can use custom port as :port in old mysql connection method
  19. if (extension_loaded('mysql')) {
  20. if (!ispos($host, ':')) {
  21. $this->host = $host . ':' . $this->dbport;
  22. } else {
  23. $this->host = $host;
  24. }
  25. } else {
  26. //mysqli use separate parameter port on connection
  27. $this->host = $host;
  28. }
  29. $this->user = $user;
  30. $this->password = $password;
  31. $this->database = $database;
  32. $this->persistent = $persistent;
  33. $this->error_reporting = $error_reporting;
  34. }
  35. public function open() {
  36. if (extension_loaded('mysql')) {
  37. if ($this->persistent) {
  38. $func = 'mysql_pconnect';
  39. } else {
  40. $func = 'mysql_connect';
  41. }
  42. $this->conn = $func($this->host, $this->user, $this->password);
  43. if (!$this->conn) {
  44. return false;
  45. }
  46. if (@!mysql_select_db($this->database, $this->conn)) {
  47. return false;
  48. }
  49. } else {
  50. $this->conn = new mysqli($this->host, $this->user, $this->password, $this->database, $this->dbport);
  51. if ($this->conn->connect_error) {
  52. return false;
  53. }
  54. }
  55. return true;
  56. }
  57. public function close() {
  58. if (extension_loaded('mysql')) {
  59. return (@mysql_close($this->conn));
  60. } else {
  61. return (@$this->conn->close());
  62. }
  63. }
  64. public function error() {
  65. if ($this->error_reporting) {
  66. if (extension_loaded('mysql')) {
  67. return (mysql_error());
  68. } else {
  69. return ($this->conn->error);
  70. }
  71. }
  72. }
  73. public function query($sql) {
  74. if (extension_loaded('mysql')) {
  75. $this->result = @mysql_query($sql, $this->conn);
  76. } else {
  77. mysqli_report(0); //TODO: make here normal fix for PHP 8.2
  78. $this->result = @$this->conn->query($sql);
  79. }
  80. return($this->result != false);
  81. }
  82. public function affectedrows() {
  83. if (extension_loaded('mysql')) {
  84. return(@mysql_affected_rows($this->conn));
  85. } else {
  86. return(@$this->conn->affected_rows);
  87. }
  88. }
  89. public function numrows() {
  90. if (extension_loaded('mysql')) {
  91. return(@mysql_num_rows($this->result));
  92. } else {
  93. return(@$this->conn->num_rows);
  94. }
  95. }
  96. public function fetchobject() {
  97. if (extension_loaded('mysql')) {
  98. return(@mysql_fetch_object($this->result));
  99. } else {
  100. return($result = @$this->result->fetch_object());
  101. }
  102. }
  103. public function fetcharray() {
  104. if (extension_loaded('mysql')) {
  105. return(mysql_fetch_array($this->result));
  106. } else {
  107. return($result = @$this->result->fetch_array(MYSQLI_BOTH));
  108. }
  109. }
  110. public function fetchassoc() {
  111. if (extension_loaded('mysql')) {
  112. return(@mysql_fetch_assoc($this->result));
  113. } else {
  114. return($result = @$this->result->fetch_assoc());
  115. }
  116. }
  117. public function freeresult() {
  118. if (extension_loaded('mysql')) {
  119. return(@mysql_free_result($this->result));
  120. } else {
  121. return(@$this->result->free());
  122. }
  123. }
  124. }
  125. ?>