mysqli.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. class Db_Mysqli implements IDb {
  3. private $link;
  4. function connect($host, $user, $pass, $db, $port) {
  5. if ($port)
  6. $this->link = mysqli_connect($host, $user, $pass, $db, $port);
  7. else
  8. $this->link = mysqli_connect($host, $user, $pass, $db);
  9. if ($this->link) {
  10. $this->init();
  11. return $this->link;
  12. } else {
  13. die("Unable to connect to database (as $user to $host, database $db): " . mysqli_connect_error());
  14. }
  15. }
  16. function escape_string($s, $strip_tags = true) {
  17. if ($strip_tags) $s = strip_tags($s);
  18. return mysqli_real_escape_string($this->link, $s);
  19. }
  20. function query($query, $die_on_error = true) {
  21. $result = @mysqli_query($this->link, $query);
  22. if (!$result) {
  23. $error = @mysqli_error($this->link);
  24. @mysqli_query($this->link, "ROLLBACK");
  25. user_error("Query $query failed: " . ($this->link ? $error : "No connection"),
  26. $die_on_error ? E_USER_ERROR : E_USER_WARNING);
  27. }
  28. return $result;
  29. }
  30. function fetch_assoc($result) {
  31. return mysqli_fetch_assoc($result);
  32. }
  33. function num_rows($result) {
  34. return mysqli_num_rows($result);
  35. }
  36. function fetch_result($result, $row, $param) {
  37. if (mysqli_data_seek($result, $row)) {
  38. $line = mysqli_fetch_assoc($result);
  39. return $line[$param];
  40. } else {
  41. return false;
  42. }
  43. }
  44. function close() {
  45. return mysqli_close($this->link);
  46. }
  47. function affected_rows($result) {
  48. return mysqli_affected_rows($this->link);
  49. }
  50. function last_error() {
  51. return mysqli_error();
  52. }
  53. function init() {
  54. $this->query("SET time_zone = '+0:0'");
  55. if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
  56. $this->query("SET NAMES " . MYSQL_CHARSET);
  57. }
  58. return true;
  59. }
  60. }
  61. ?>