Guest User

Untitled

a guest
May 1st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. <?php
  2. class CP_MySQL
  3. {
  4. //Member data
  5. var $connection = ""; //Holds the MySQL connection returned from mysql_connect()
  6. var $db = ""; //Holds the MySQL database selction from mysql_select_db()
  7. var $result = ""; //Holds the MySQL resource from mysql_query()
  8. var $query = ""; //Holds the last run query string.
  9. var $execution_time = ""; //Holds the amount of time it took to run the last query.
  10.  
  11. function __construct($info = "")
  12. {
  13. if($info != "" && is_array($info)){
  14. $this->connect($info['host'], $info['user'],$info['pass']);
  15. $this->select_db($info['database']);
  16. }
  17. }
  18.  
  19. //Member functions
  20. ///connect(string $server, string $username, string $password)
  21. ///\brief Connect to the MySQL database.
  22. ///@param $server - string - The server address.
  23. ///@param $username - string - The username to connect with.
  24. ///@param $password - string - The password to connect with.
  25. ///@return mysql_resource - Returns the resource from mysql_connect.
  26. function connect($server = "", $username = "", $password = "")
  27. {
  28.  
  29. $this->connection = mysql_connect($server, $username, $password) or debug_print_backtrace();
  30. return $this->connection;
  31. }
  32.  
  33. ///select_db(string $db)
  34. ///\brief Selects the database specfied by $db.
  35. ///@param $db - string - The database to select.
  36. ///@return mysql_resource - Returns the resource from mysql_select_db.
  37. function select_db($db = "")
  38. {
  39. $this->db = mysql_select_db($db, $this->connection) or debug_print_backtrace();
  40.  
  41. return $this->db;
  42. }
  43.  
  44. /**
  45. * query(string $query)
  46. * \brief Runs the specified query.
  47. * @param $query - string - The query to run.
  48. * @return mysql_resource - Returns the results of the query.
  49. */
  50. function query($query = "", $args = array())
  51. {
  52. $argt = array();
  53. preg_match_all("#((?<!%)|(?<=%%))%[bcdeufFosxX]#", $query, $argt, PREG_PATTERN_ORDER);
  54.  
  55. //Strip out that useless $argt[1].
  56. $argt = $argt[0];
  57.  
  58. if(count($argt) != count($args))
  59. {
  60. $cargt = count($argt);
  61. $cargs = count($args);
  62. echo "Error: Number of arguments ($cargs) not equal to number of expected arguments ($cargt).";
  63. print_r($argt);
  64. exit(0);
  65. }
  66.  
  67. $i = 0;
  68. foreach($argt as $value)
  69. {
  70. switch($value)
  71. {
  72. case '%d':
  73. if(!is_numeric($args[$i]))
  74. exit("Error: Argument $i does not match specified type. Type: " /*. print_r($args,true) . "<br />Query: " . $query */);
  75. break;
  76. case '%s':
  77. break;
  78. default:
  79. print_r($argt);
  80. exit("Error: Invalid argument type '{$value}'." . $query);
  81. break;
  82. }
  83. $i++;
  84. }
  85.  
  86. foreach($args as $value)
  87. $value = mysql_real_escape_string($value, $this->connection)or debug_print_backtrace();
  88.  
  89. $this->query = vsprintf($query, $args);
  90. $this->result = mysql_query($this->query, $this->connection) or debug_print_backtrace();
  91.  
  92. if(!$this->result)
  93. die('Invalid query: ' . $this->query . " - " . mysql_error());
  94.  
  95. return $this->result;
  96. }
  97.  
  98. ///get_result()
  99. ///\brief Returns the $results variable. The same return as query().
  100. ///@return mysql_resource - Returns the results of the last executed query.
  101. function get_result()
  102. {
  103. return $this->result;
  104. }
  105.  
  106. ///set_result(mysql_resource $result)
  107. ///\brief Set the result from an outside variable.
  108. ///@param $result - mysql_resource - The result set to set.
  109. function set_result($result)
  110. {
  111. $this->result = $result;
  112. }
  113.  
  114. ///fetch_array()
  115. ///\brief Returns the next row in the results.
  116. ///@return array - The fetched row.
  117. function fetch_array($result = 0)
  118. {
  119. if($result != 0)
  120. return mysql_fetch_array($result,MYSQL_ASSOC);
  121.  
  122. return mysql_fetch_array($this->result,MYSQL_ASSOC);
  123. }
  124.  
  125. ///num_rows()
  126. ///\brief Returns the number of rows in the results.
  127. ///@return int - Number of rows.
  128. function num_rows($result = 0)
  129. {
  130. if($result != 0)
  131. return mysql_num_rows($result)or debug_print_backtrace();
  132.  
  133. return mysql_num_rows($this->result) or debug_print_backtrace();
  134. }
  135.  
  136. ///data_seek(int $index)
  137. ///\brief Moves the internal pointer of the results to specified $index.
  138. ///@param $index - int - Index to move the pointer to. 0 >= $index > num_rows()
  139. function data_seek($index)
  140. {
  141. mysql_data_seek($this->result, $index);
  142. }
  143.  
  144. ///escape_string(string $string)
  145. ///\brief Makes a string SQL-safe
  146. ///@param $string - string - The string to be made SQL safe
  147. ///@return string - The SQL safe string
  148. function escape_string($string)
  149. {
  150. return mysql_real_escape_string($string);
  151. }
  152.  
  153. function free_result($result)
  154. {
  155. mysql_free_result($result);
  156. }
  157.  
  158. function free($result){
  159. mysql_free_result($result);
  160. }
  161. }
  162. ?>
Add Comment
Please, Sign In to add comment