mysqli.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. user_error("Query $query failed: " . ($this->link ? mysqli_error($this->link) : "No connection"),
  24. $die_on_error ? E_USER_ERROR : E_USER_WARNING);
  25. }
  26. return $result;
  27. }
  28. function fetch_assoc($result) {
  29. return mysqli_fetch_assoc($result);
  30. }
  31. function num_rows($result) {
  32. return mysqli_num_rows($result);
  33. }
  34. function fetch_result($result, $row, $param) {
  35. if (mysqli_data_seek($result, $row)) {
  36. $line = mysqli_fetch_assoc($result);
  37. return $line[$param];
  38. } else {
  39. return false;
  40. }
  41. }
  42. function close() {
  43. return mysqli_close($this->link);
  44. }
  45. function affected_rows($result) {
  46. return mysqli_affected_rows($this->link);
  47. }
  48. function last_error() {
  49. return mysqli_error();
  50. }
  51. function init() {
  52. $this->query("SET time_zone = '+0:0'");
  53. if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
  54. $this->query("SET NAMES " . MYSQL_CHARSET);
  55. }
  56. return true;
  57. }
  58. }
  59. ?>