Guest User

Untitled

a guest
May 29th, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. <?php
  2. /**
  3. * Database Wrapper
  4. * @version $Id$
  5. * @author Moazzam Khan <mkhan@talkpoint.com>
  6. * @package TPFramework
  7. */
  8.  
  9. /**
  10. * Database wrapper (singleton)
  11. *
  12. * @author Moazzam Khan <mkhan@talkpoint.com>
  13. * @package TPFramework
  14. */
  15. class database {
  16. /** @var DB connection resource handle */
  17. private $_resource;
  18. /** @var string Internal variable to hold SQL */
  19. private $_sql;
  20. /** @var Internal variable to hold last query cursor */
  21. private $_cursor;
  22. /** @var Array Log of queries */
  23. private $_log = array();
  24. /** @var int Internal variable to hold the error number of last error */
  25. private $_errorNum;
  26. /** @var string Internal variable to hold the error number of last error */
  27. private $_errorMsg;
  28. /** @var debugging flag */
  29. public $debug = 0;
  30.  
  31. /**
  32. * Constructor
  33. *
  34. * @param string $host
  35. * @param string $user
  36. * @param string $pass
  37. * @param string $name
  38. * @return database
  39. */
  40. private function database($host, $user, $pass, $name)
  41. {
  42. if (!($this->_resource = @mysql_pconnect($host, $user, $pass))) {
  43. trigger_error(mysql_error(), E_USER_ERROR);
  44. return;
  45. }
  46. if (!mysql_select_db($name)) {
  47. trigger_error("Database Selection failed", E_USER_ERROR);
  48. }
  49. }
  50.  
  51. /**
  52. * @param string $host DB Hostname/IP
  53. * @param string $user DB Username
  54. * @param string $pass DB User's password
  55. * @param string $name DB Name
  56. * @return object
  57. */
  58. static function &getInstance($host='', $user='', $pass='', $name='')
  59. {
  60. static $db;
  61. if (!is_object($db))
  62. $db = new database($host, $user, $pass, $name);
  63. return $db;
  64. }
  65.  
  66.  
  67. function execute($sql)
  68. {
  69. if (!$this->query($sql)) return false;
  70. return true;
  71. }
  72.  
  73. /**
  74. * @param string $sql SQL statement
  75. * @return Array of rows returned by the query
  76. */
  77. function getAll($sql)
  78. {
  79. $this->_sql = $sql;
  80. $ret =& $this->retrieveResults('', 0, 'assoc');
  81. return $ret;
  82. }
  83.  
  84. /**
  85. * Returns an array of row objects
  86. * @param string $sql
  87. * @see retrieveResults
  88. */
  89. function loadObjectList($sql)
  90. {
  91. return $this->retrieveResults('', 0, 'object');
  92. }
  93.  
  94.  
  95. /**
  96. * Load an array of retrieved database objects or values
  97. * @param int Database cursor
  98. * @param string The field name of a primary key
  99. * @return array If <var>key</var> is empty as sequential list of returned records.
  100. * If <var>key</var> is not empty then the returned array is indexed by the value
  101. * the database key. Returns <var>null</var> if the query fails.
  102. */
  103. function &retrieveResults ($key='', $max=0, $result_type='row')
  104. {
  105. $results = array();
  106. $sql_function = 'mysql_fetch_'.$result_type;
  107. if ($cur = $this->query()) {
  108. while ($row = $sql_function($cur)) {
  109. if ($key != '') $results[$row->$key] = $row;
  110. else $results[] = $row;
  111. if ($max AND count($results) >= $max) break;
  112. }
  113. mysql_free_result($cur);
  114. }
  115. return $results;
  116. }
  117.  
  118. /**
  119. * Execute the query
  120. * @return mixed A database resource if successful, FALSE if not.
  121. */
  122. function query($sql = '')
  123. {
  124. $this->_errorNum = 0;
  125. $this->_errorMsg = '';
  126. if ($sql == '') $sql = $this->_sql;
  127. if ($this->debug) $this->_log[] = $sql;
  128. if ($this->_cursor = mysql_query($sql, $this->_resource)) {
  129. $this->_errorNum = 0;
  130. $this->_errorMsg = '';
  131. return $this->_cursor;
  132. } else {
  133. $this->_errorNum = mysql_errno( $this->_resource );
  134. $this->_errorMsg = mysql_error( $this->_resource )." SQL=$sql";
  135. //if ($this->_debug) $this->debug_trace();
  136. return false;
  137. }
  138. }
  139.  
  140. function errorMsg() {return $this->_errorMsg;}
  141.  
  142.  
  143. /** Closes the db connection */
  144. function close()
  145. {
  146. @mysql_close($this->_resource);
  147. $this->_resource = NULL;
  148. }
  149.  
  150. }
Add Comment
Please, Sign In to add comment