PageRenderTime 255ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/yitai/json-rpc/libdb.php

http://jqian.googlecode.com/
PHP | 138 lines | 91 code | 20 blank | 27 comment | 13 complexity | 2472149b0874780fdce9731b722ab956 MD5 | raw file
  1. <?php
  2. // libdb.php --- Time-stamp: <2010-09-30 11:50:57 ??? by lancer>
  3. // Copyright 2010 Julian Qian
  4. // Author: lancer@yitai01
  5. // Version: $Id: libdb.php,v 0.0 2010/09/30 01:57:15 lancer Exp $
  6. // Keywords:
  7. // Samples:
  8. // $db = new Database("localhost", "username", "password", "database");
  9. // Sample for update/insert/delete query
  10. // $db->connect();
  11. // $db->query("update db .. ");
  12. // $db->close();
  13. // Sample for select query all rows return
  14. // $db->connect();
  15. // $db->fetch_all("select db .. ");
  16. // $db->close();
  17. // Sample for select only first row return
  18. // $db->connect();
  19. // $db->fetch_first("select db .. ");
  20. // $db->close();
  21. class Database {
  22. private $server = "localhost";
  23. private $user = "";
  24. private $pass = "";
  25. private $database = "";
  26. private $link_id;
  27. private $query_id;
  28. private $affected_rows;
  29. public function __construct($server, $user, $pass, $database){
  30. $this->server = $server;
  31. $this->user = $user;
  32. $this->pass = $pass;
  33. $this->database = $database;
  34. }
  35. public function connect($new_link = false){
  36. $this->link_id = @mysql_pconnect($this->server,
  37. $this->user,
  38. $this->pass,
  39. $new_link);
  40. if (! $this->link_id){
  41. throw new Exception("Could not connect to server: <strong>" . $this->server. "</strong>");
  42. }
  43. if (! @mysql_select_db($this->database, $this->link_id)){
  44. throw new Exception("Could not open database: <strong>" . $this->database. "</strong>");
  45. }
  46. unset($this->server,
  47. $this->user,
  48. $this->pass,
  49. $this->database);
  50. }
  51. public function close() {
  52. if( ! @mysql_close($this->link_id)){
  53. throw new Exception("Connection close failed.");
  54. }
  55. }
  56. // for "INSERT/UPDATE/DELETE ...", will return TRUE/FALSE
  57. // for "SELECT ..", will return resource_handle/FALSE
  58. public function query($sql) {
  59. // do query
  60. $this->query_id = @mysql_query($sql, $this->link_id);
  61. if (!$this->query_id) {
  62. throw new Exception("<b>MySQL Query failed:</b> $sql");
  63. }
  64. // $this->affected_rows = @mysql_affected_rows($this->link_id);
  65. return $this->query_id;
  66. }
  67. // for "SELECT ...", no need to call query()
  68. public function fetch_all($sql) {
  69. $query_id = $this->query($sql);
  70. $out = array();
  71. while ($row = $this->_fetch_array($query_id)){
  72. $out[] = $row;
  73. }
  74. $this->_free_result($query_id);
  75. return $out;
  76. }
  77. // for "SELECT ...", only return first row
  78. public function fetch_first($sql) {
  79. $query_id = $this->query($sql);
  80. $out = $this->_fetch_array($query_id);
  81. $this->_free_result($query_id);
  82. return $out;
  83. }
  84. public function query_num_rows(){
  85. $query_id = $this->query($sql);
  86. $num = mysql_num_row($query_id);
  87. $this->_free_result($query_id);
  88. return $num;
  89. }
  90. // private methods
  91. private function _escape($string) {
  92. if(get_magic_quotes_runtime()) $string = stripslashes($string);
  93. return @mysql_real_escape_string($string,$this->link_id);
  94. }
  95. private function _fetch_array($query_id = -1) {
  96. // retrieve row
  97. if ($query_id != -1) {
  98. $this->query_id = $query_id;
  99. }
  100. if (isset($this->query_id)) {
  101. $record = @mysql_fetch_assoc($this->query_id);
  102. }else{
  103. throw new Exception("Invalid query_id: <b>". $this->query_id.
  104. "</b>. Records could not be fetched.");
  105. }
  106. return $record;
  107. }
  108. private function _free_result($query_id=-1) {
  109. if ($query_id!=-1) {
  110. $this->query_id = $query_id;
  111. }
  112. if($this->query_id != 0 && !@mysql_free_result($this->query_id)) {
  113. throw new Exception("Result ID: <b>". $this->query_id.
  114. "</b> could not be freed.");
  115. }
  116. }
  117. }
  118. ?>