Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. <?
  2. /**
  3. * database layer for mysql
  4. */
  5.  
  6.  
  7.  
  8. class db
  9. {
  10. /**
  11. * @var string database
  12. * @access private
  13. */
  14. var $database = "";
  15.  
  16.  
  17. /**
  18. * @var string server
  19. * @access private
  20. */
  21. var $server = "";
  22.  
  23.  
  24. /**
  25. * @var string user
  26. * @access private
  27. */
  28. var $user = "";
  29.  
  30.  
  31. /**
  32. * @var string password
  33. * @access private
  34. */
  35. var $password = "";
  36.  
  37.  
  38. /**
  39. * @var string tableprefix
  40. * @access private
  41. */
  42. var $tableprefix = "webpm_";
  43.  
  44.  
  45. /**
  46. * @var resource link_id stores the mysql link identifier
  47. * @access private
  48. */
  49. var $link_id = 0;
  50.  
  51.  
  52.  
  53. /**
  54. * @var integer querycounter
  55. * @access private
  56. */
  57. var $querycounter = 0;
  58.  
  59.  
  60. /**
  61. * @var array queries
  62. * @access private
  63. */
  64. var $queries=array();
  65.  
  66.  
  67.  
  68. /**
  69. * constructor
  70. *
  71. * @param string server
  72. * @param string user
  73. * @param string password
  74. * @param string database
  75. * @param string tableprefix
  76. */
  77. function db($server,$user,$password,$database,$tableprefix)
  78. {
  79. $this->server=$server;
  80. $this->user=$user;
  81. $this->password=$password;
  82. $this->database=$database;
  83. $this->tableprefix=$tableprefix;
  84. $this->querycounter=0;
  85. $this->queries=array();
  86. $this->querytimer=array();
  87.  
  88. $this->link_id=0;
  89. $this->connect();
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96. /**
  97. * open a connection to the database server
  98. *
  99. * @return void
  100. * @access private
  101. */
  102. function connect()
  103. {
  104. if($this->link_id==0)
  105. {
  106. $this->link_id=mysql_connect($this->server,$this->user,$this->password);
  107. if(!$this->link_id)
  108. {
  109. $this->error("cannot connect to database server ".$this->server);
  110. }
  111. if($this->database!="")
  112. {
  113. $this->select_db();
  114. }
  115. }
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122. /**
  123. * selects a database
  124. *
  125. * @param string databse
  126. * @return void
  127. * @access public
  128. */
  129. function select_db($database="")
  130. {
  131. if($database!="")
  132. {
  133. $this->database=$database;
  134. }
  135. if(!@mysql_select_db($this->database, $this->link_id))
  136. {
  137. $this->error("cannot use database ".$this->database);
  138. }
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. /**
  147. * sends and execute a sql query
  148. *
  149. * @param string query_string
  150. * @return resource result
  151. * @access public
  152. */
  153. function query($query_string)
  154. {
  155. $result = mysql_query($query_string,$this->link_id);
  156. $this->queries[]=$query_string;
  157. if(!$result)
  158. {
  159. $this->error("Invalid SQL: ".$query_string);
  160. }
  161. $this->querycounter++;
  162. return $result;
  163. }
  164.  
  165.  
  166.  
  167. /**
  168. * fetches a result row as an associative array, a numeric array, or both
  169. *
  170. * @param resource result
  171. * @param integer result_type
  172. * @return array result row
  173. * @access public
  174. */
  175. function fetch_array($result,$result_type=MYSQL_BOTH)
  176. {
  177. return @mysql_fetch_array($result,$result_type);
  178. }
  179.  
  180.  
  181.  
  182.  
  183. /**
  184. * free result memory
  185. *
  186. * @param resource result
  187. * @return boolean
  188. * @access public
  189. */
  190. function free_result($result)
  191. {
  192. return @mysql_free_result($result);
  193. }
  194.  
  195.  
  196.  
  197.  
  198. /**
  199. * sends a sql query and fetches the first result row as an associative array, a numeric array, or both
  200. *
  201. * @param string query_string
  202. * @param integer result_type
  203. * @return array result row
  204. * @access public
  205. */
  206. function query_first($query_string,$result_type=MYSQL_BOTH)
  207. {
  208. $result = $this->query($query_string);
  209. $returnarray=$this->fetch_array($result,$result_type);
  210. $this->free_result($result);
  211. return $returnarray;
  212. }
  213.  
  214.  
  215.  
  216.  
  217. /**
  218. * sends an SQL query to MySQL, without fetching and buffering the result rows
  219. *
  220. * @param string query_string
  221. * @param integer low_priority
  222. * @return resource result
  223. * @access public
  224. */
  225. function unbuffered_query($query_string,$low_priority=0)
  226. {
  227. global $phpversion;
  228.  
  229. if($phpversion<406)
  230. {
  231. return $this->query($query_string);
  232. }
  233. else
  234. {
  235. if($low_priority==1)
  236. {
  237. $query_string=preg_replace("/^(REPLACE|INSERT|UPDATE|DELETE)/im","\\1 LOW_PRIORITY", $query_string);
  238. }
  239. $result = mysql_unbuffered_query($query_string,$this->link_id);
  240. $this->queries[]="unbuffered: $query_string";
  241. if(!$result) $this->error("Invalid SQL: ".$query_string);
  242. $this->querycounter++;
  243. return $result;
  244. }
  245. }
  246.  
  247.  
  248.  
  249.  
  250. /**
  251. * returns the number of rows in result
  252. *
  253. * @param resource result
  254. * @return integer number_of_rows
  255. * @access public
  256. */
  257. function num_rows($result)
  258. {
  259. return @mysql_num_rows($result);
  260. }
  261.  
  262.  
  263.  
  264.  
  265. /**
  266. * returns the number of affected rows in previous MySQL operation
  267. *
  268. * @return integer affected_rows
  269. * @access public
  270. */
  271. function affected_rows()
  272. {
  273. return mysql_affected_rows($this->link_id);
  274. }
  275.  
  276.  
  277.  
  278.  
  279. /**
  280. * returns the primary key generated from the previous INSERT operation
  281. *
  282. * @return integer id
  283. * @access public
  284. */
  285. function insert_id()
  286. {
  287. return mysql_insert_id($this->link_id);
  288. }
  289.  
  290.  
  291.  
  292. /**
  293. * dies with an error message
  294. *
  295. * @param string msg error message
  296. * @return void;
  297. * @access private
  298. */
  299. function error($msg)
  300. {
  301. die("<font face=Verdana size=2><b>Database error</b><br>
  302. $msg<br><br>
  303. mysql error: ".mysql_error()."<br>
  304. mysql error no: ".mysql_errno());
  305. }
  306.  
  307.  
  308.  
  309.  
  310. /**
  311. * returns the tableprefix
  312. *
  313. * @return string tableprefix
  314. * @access public
  315. */
  316. function getPrefix()
  317. {
  318. return $this->tableprefix;
  319. }
  320.  
  321.  
  322.  
  323.  
  324. /**
  325. * print outs the number of sql queries and the executed sql queries.
  326. *
  327. * @return void
  328. * @access public
  329. */
  330. function debug()
  331. {
  332. echo "<p align='center' class='small'>SQL Queries: ".$this->querycounter."</p>\n";
  333. echo "<ul class='small'>\n";
  334. for($i=0;$i<$this->querycounter;$i++) echo "<li>".$this->queries[$i]."</li>\n";
  335. echo "</ul>\n";
  336. }
  337. }
  338. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement