Guest User

Untitled

a guest
Jun 1st, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.85 KB | None | 0 0
  1. <?php
  2. /**
  3. * Support for MySQL, MySQLi, MS SQL & PostgreSQL
  4. *
  5. * @author Richard Rowe <inbox@richardrowe.net>
  6. *
  7. */
  8.  
  9. /**
  10. * @todo Improve error handling
  11. * @todo Some bug fixing / testing in mysqli class
  12. * @todo Test MS SQL, MySQL & PostgreSQL classes
  13. */
  14.  
  15. interface db_base
  16. {
  17. public function __construct($host = null, $user = null, $pass = null, $dbname = null);
  18. public function close();
  19. public function affected_rows();
  20. public function fetchrow($result = false);
  21. public function freeresult($result = false);
  22. public function insert_id();
  23. public function num_rows($result = false);
  24. public function query($sql);
  25. public function escape_string($str);
  26. public function error($str);
  27. }
  28.  
  29.  
  30. class db_mssql implements db_base
  31. {
  32. public $link;
  33. public $result;
  34. public $error;
  35.  
  36. public function __construct($host = null, $user = null, $pass = null, $dbname = null)
  37. {
  38. /**
  39. * Open MS SQL server connection
  40. */
  41. if (!$this->link = mssql_connect($host, $user, $pass))
  42. {
  43. // error!
  44. trigger_error('Could not connect to the database.', E_USER_ERROR);
  45. }
  46.  
  47. /**
  48. * Select MS SQL database
  49. */
  50. if (!mssql_select_db($dbname, $this->link))
  51. {
  52. // error!
  53. trigger_error('Could not select the database.', E_USER_ERROR);
  54. }
  55. }
  56.  
  57. /**
  58. * Close MS SQL Server connection
  59. */
  60. public function close()
  61. {
  62. if (!mssql_close($this->link))
  63. {
  64. // error!
  65. trigger_error('Could not close the database.', E_USER_ERROR);
  66. }
  67.  
  68. return true;
  69. }
  70.  
  71. /**
  72. * Returns the number of records affected by the query
  73. */
  74. public function affected_rows()
  75. {
  76. return $this->link ? mssql_rows_effected($this->link) : false;
  77. }
  78.  
  79. /**
  80. * Get row as enumerated array
  81. */
  82. public function fetchrow($result = false)
  83. {
  84. return $result ? mssql_fetch_assoc($result) : mssql_fetch_assoc($this->result);
  85. }
  86.  
  87. /**
  88. * Free result memory
  89. */
  90. public function freeresult($result = false)
  91. {
  92. return $result ? mssql_free_result($result) : mssql_free_result($this->result);
  93. }
  94.  
  95. /**
  96. * Method taken from phpBB.
  97. */
  98. public function insert_id()
  99. {
  100. if ($result = mssql_query('SELECT SCOPE_IDENTITY()', $this->link))
  101. {
  102. if ($row = mssql_fetch_assoc($result))
  103. {
  104. mssql_free_result($result);
  105. return $row['computed'];
  106. }
  107.  
  108. mssql_free_result($result);
  109. }
  110.  
  111. return false;
  112. }
  113.  
  114. /**
  115. * Gets the number of rows in result
  116. */
  117. public function num_rows($result = false)
  118. {
  119. return $result ? mssql_num_rows($result) : mssql_num_rows($this->result);
  120. }
  121.  
  122. /**
  123. * Send MS SQL query
  124. */
  125. public function query($sql)
  126. {
  127. if (!$this->result = mssql_query($sql, $this->link))
  128. {
  129. // error!
  130. //trigger_error('<strong>Query error:</strong><br />', E_USER_ERROR);
  131. }
  132.  
  133. return $this->result;
  134. }
  135.  
  136. /**
  137. * Escapes special characters in a string for use in a SQL statement
  138. *
  139. * @param string $str The string that is to be escaped.
  140. * @return string Returns the escaped string.
  141. */
  142. public function escape_string($str)
  143. {
  144. // Magic Quotes escapes all incoming data to the PHP script.
  145. if (ini_get('magic_quotes_gpc'))
  146. {
  147. // To avoid escaping the string twice, stripslashes
  148. $str = stripslashes($str);
  149. }
  150.  
  151. return str_replace('\'', '\'\'', $str);
  152. }
  153.  
  154. public function error($str)
  155. {
  156. return $this->error = $str;
  157. }
  158. }
  159.  
  160.  
  161. /**
  162. * @todo public function insert_id()
  163. */
  164. class db_postgresql implements db_base
  165. {
  166. public $link;
  167. public $result;
  168. public $error;
  169.  
  170. public function __construct($host = null, $user = null, $pass = null, $dbname = null)
  171. {
  172. if (!$this->link = pg_connect("host = $host, dbname = $dbname, user = $user, password = $pass"))
  173. {
  174. // error!
  175. trigger_error('Could not connect and / or select the database.', E_USER_ERROR);
  176. }
  177. }
  178.  
  179. /**
  180. * Closes a PostgreSQL connection
  181. */
  182. public function close()
  183. {
  184. if (!pg_close($this->link))
  185. {
  186. // error!
  187. trigger_error('Could nto close the database.', E_USER_ERROR);
  188. }
  189.  
  190. return true;
  191. }
  192.  
  193. /**
  194. * Returns number of affected records (tuples)
  195. */
  196. public function affected_rows()
  197. {
  198. return $this->link ? pg_affected_rows($this->link) : false;
  199. }
  200.  
  201. /**
  202. * Fetch a row as an associative array
  203. */
  204. public function fetchrow($result = false)
  205. {
  206. return $result ? pg_fetch_assoc($result) : pg_fetch_assoc($this->result);
  207. }
  208.  
  209. /**
  210. * Free result memory
  211. */
  212. public function freeresult($result = false)
  213. {
  214. return $result ? pg_free_result($result) : pg_free_result($this->result);
  215. }
  216.  
  217. /**
  218. *
  219. */
  220. public function insert_id()
  221. {
  222. //return pg_last_oid($this->link);
  223. }
  224.  
  225. /**
  226. * Returns the number of rows in a result
  227. */
  228. public function num_rows($result = false)
  229. {
  230. return $result ? pg_num_rows($result) : pg_num_rows($this->result);
  231. }
  232.  
  233. /**
  234. * Execute a query
  235. */
  236. public function query($sql)
  237. {
  238. if (!$this->result = pg_query($this->link, $sql))
  239. {
  240. // error!
  241. trigger_error('<strong>Query failed:</strong><br />' . pg_result_error($this->result), E_USER_ERROR);
  242. }
  243.  
  244. return $this->result;
  245. }
  246.  
  247. /**
  248. * Escape a string for insertion into a text field
  249. *
  250. * @param string $str A string containing text to be escaped.
  251. * @return A string containing the escaped data.
  252. */
  253. public function escape_string($str)
  254. {
  255. // Magic Quotes escapes all incoming data to the PHP script.
  256. if (ini_get('magic_quotes_gpc'))
  257. {
  258. // To avoid escaping the string twice, stripslashes
  259. $str = stripslashes($str);
  260. }
  261.  
  262. return pg_escape_string($this->link, $str);
  263. }
  264.  
  265. public function error($str)
  266. {
  267. return $this->error = $str;
  268. }
  269. }
  270.  
  271.  
  272. class db_mysql implements db_base
  273. {
  274. public $link;
  275. public $result;
  276. public $error;
  277.  
  278. public function __construct($host = null, $user = null, $pass = null, $dbname = null)
  279. {
  280. // Open a connection to a MySQL Server
  281. if (!$this->link = mysql_connect($host, $user, $pass, true))
  282. {
  283. trigger_error('<strong>Could not connect to the database:</strong><br />' . mysql_error(), E_USER_ERROR);
  284. }
  285.  
  286. // Select a MySQL database
  287. if (!mysql_select_db($dbname, $this->link))
  288. {
  289. trigger_error('<strong>Could not select the database:</strong><br />' . mysql_error(), E_USER_ERROR);
  290. }
  291. }
  292.  
  293. /**
  294. * Close MySQL connection
  295. */
  296. public function close()
  297. {
  298. if (!mysql_close($this->link))
  299. {
  300. // error!
  301. trigger_error('Could not close the database.', E_USER_ERROR);
  302. }
  303.  
  304. return true;
  305. }
  306.  
  307. /**
  308. * Get number of affected rows in previous MySQL operation
  309. */
  310. public function affected_rows()
  311. {
  312. return $this->link ? mysql_affected_rows($this->link) : false;
  313. }
  314.  
  315. /**
  316. * Fetch a result row as an associative array
  317. *
  318. * @param resource $result The result resource that is being evaluated. This result comes from a call to mysql_query().
  319. */
  320. public function fetchrow($result = false)
  321. {
  322. return $result ? mysql_fetch_assoc($result) : mysql_fetch_assoc($this->result);
  323. }
  324.  
  325. /**
  326. * Free result memory
  327. *
  328. * @param resource $result The result resource that is being evaluated. This result comes from a call to mysql_query().
  329. */
  330. public function freeresult($result = false)
  331. {
  332. return $result ? mysql_free_result($result) : mysql_free_result($this->result);
  333. }
  334.  
  335. /**
  336. * Get the ID generated from the previous INSERT operation
  337. */
  338. public function insert_id()
  339. {
  340. return $this->link ? mysql_insert_id($this->link) : false;
  341. }
  342.  
  343. /**
  344. * Get number of rows in result
  345. *
  346. * @param resource $result The result resource that is being evaluated. This result comes from a call to mysql_query().
  347. */
  348. public function num_rows($result = false)
  349. {
  350. return $result ? mysql_num_rows($result) : mysql_num_rows($this->result);
  351. }
  352.  
  353. /**
  354. * Send a MySQL query
  355. *
  356. * @param string $sql A SQL query. The query string should not end with a semicolon.
  357. */
  358. public function query($sql)
  359. {
  360. if (!$this->result = mysql_db_query($sql, $this->link))
  361. {
  362. // error!
  363. trigger_error('<strong>Query error:</strong><br />' . mysql_error($this->link), E_USER_ERROR);
  364. }
  365.  
  366. return $this->result;
  367. }
  368.  
  369. /**
  370. * Escapes special characters in a string for use in a SQL statement
  371. *
  372. * @param string $str The string that is to be escaped.
  373. * @return string Returns the escaped string, or FALSE on error.
  374. */
  375. public function escape_string($str)
  376. {
  377. // Magic Quotes escapes all incoming data to the PHP script.
  378. if (ini_get('magic_quotes_gpc'))
  379. {
  380. // To avoid escaping the string twice, stripslashes
  381. $str = stripslashes($str);
  382. }
  383.  
  384. return mysql_real_escape_string($str, $this->link);
  385. }
  386.  
  387. public function error($str)
  388. {
  389. return $this->error = $str;
  390. }
  391. }
  392.  
  393.  
  394. class db_mysqli implements db_base
  395. {
  396. public $link;
  397. public $result;
  398. public $errors;
  399.  
  400. public function __construct($host = null, $user = null, $pass = null, $dbname = null)
  401. {
  402. if (!$this->link = new mysqli($host, $user, $pass, $dbname))
  403. {
  404. // error!
  405. trigger_error('Could not connect to the database', E_USER_ERROR);
  406. }
  407. }
  408.  
  409. /**
  410. * Closes a previously opened database connection
  411. */
  412. public function close()
  413. {
  414. if (!$this->link->close())
  415. {
  416. // error!
  417. trigger_error('Failed to close database.', E_USER_ERROR);
  418. }
  419.  
  420. return true;
  421. }
  422.  
  423. /**
  424. * Gets the number of affected rows in a previous MySQL operation
  425. */
  426. public function affected_rows()
  427. {
  428. return $this->link ? $this->link->affected_rows() : false;
  429. }
  430.  
  431.  
  432. /**
  433. * Fetch a result row as an associative array
  434. *
  435. * @param $result Returned by $this->query($sql)
  436. */
  437. public function fetchrow($result = false)
  438. {
  439. return $result ? $result->fetch_assoc() : $this->link->fetch_assoc();
  440. }
  441.  
  442. /**
  443. * Frees the memory associated with a result
  444. *
  445. * @param $result Returned by $this->query($sql)
  446. */
  447. public function freeresult($result = false)
  448. {
  449. return $result ? $result->free() : $this->result->free();
  450. }
  451.  
  452. /**
  453. * Returns the auto generated id used in the last query
  454. */
  455. public function insert_id()
  456. {
  457. return $this->link ? $this->link->insert_id() : false;
  458. }
  459.  
  460. /**
  461. * Gets the number of rows in a result
  462. *
  463. * @param $result Returned by $this->query($sql)
  464. */
  465. public function num_rows($result = false)
  466. {
  467. //$result->num_rows()
  468. return $result ? mysqli_num_rows($result) : $this->result->num_rows();
  469. }
  470.  
  471. /**
  472. * Performs a query on the database
  473. *
  474. * @param $sql The query string
  475. */
  476. public function query($sql)
  477. {
  478. if (!$this->result = $this->link->query($sql))
  479. {
  480. // error!
  481. trigger_error('<strong>Query error:</strong><br />' . mysqli_error($this->link) . '<br />File: ' . __FILE__ . '<br />Line: ' . __LINE__, E_USER_ERROR);
  482. }
  483.  
  484. return $this->result;
  485. }
  486.  
  487. /**
  488. * Escapes special characters in a string for use in a SQL statement, taking into account the current charset of the connection
  489. *
  490. * @param string $str The string to be escaped.
  491. * @return string Returns an escaped string.
  492. */
  493. public function escape_string($str)
  494. {
  495. // Magic Quotes escapes all incoming data to the PHP script.
  496. if (ini_get('magic_quotes_gpc'))
  497. {
  498. // To avoid escaping the string twice, we use stripslashes
  499. $str = stripslashes($str);
  500. }
  501.  
  502. return $this->link->real_escape_string($str);
  503. }
  504.  
  505. public function error($str)
  506. {
  507. return $this->error = $str;
  508. }
  509. }
  510.  
  511. ?>
Add Comment
Please, Sign In to add comment