MySql.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. /**
  8. * Description of MySql
  9. *
  10. * @author jgao
  11. */
  12. class MySql {
  13. //put your code here
  14. var $conn = 0;
  15. function conn($dbhost, $dbport, $dbuser, $dbpw, $dbname) {
  16. $this->conn = mysql_connect($dbhost . ":" . $dbport, $dbuser, $dbpw);
  17. if ($this->conn == null || !$this->conn) {
  18. $this->halt("Connect to MySQL failed");
  19. }
  20. $serverinfo = mysql_get_server_info($this->conn);
  21. if ($serverinfo > '4.1' && $GLOBALS['charset']) {
  22. mysql_query("SET character_set_connection=" . $GLOBALS['charset'] . ",character_set_results=" . $GLOBALS['charset'] . ",character_set_client=binary", $this->conn);
  23. }
  24. if ($serverinfo > '5.0') {
  25. mysql_query("SET sql_mode=''", $this->conn);
  26. }
  27. if ($dbname && !@mysql_select_db($dbname, $this->conn)) {
  28. $this->halt('Cannot use database');
  29. }
  30. }
  31. function select_db($dbname) {
  32. if (!@mysql_select_db($dbname, $this->conn)) {
  33. $this->halt('Cannot use database');
  34. }
  35. }
  36. function server_info() {
  37. return mysql_get_server_info($this->conn);
  38. }
  39. function insert_id() {
  40. $arr = $this->fetch_array('SELECT LAST_INSERT_ID() as id');
  41. return $arr["id"];
  42. }
  43. function get_value($SQL, $offset = 0, $field = 0) {
  44. $rt = $this->fetch_result($SQL);
  45. if (isset($rt[$offset][$field])) {
  46. return $rt[$offset][$field];
  47. }
  48. return false;
  49. }
  50. function query($SQL, $method = null, $error = true) {
  51. $query = mysql_query($SQL, $this->conn);
  52. !$query && $error && $this->halt('Query Error: ' . $SQL);
  53. return $query;
  54. }
  55. function fetch_row($SQL) {
  56. return $this->fetch_result($SQL, MYSQL_NUM);
  57. }
  58. function fetch_array($SQL) {
  59. return $this->fetch_result($SQL, MYSQL_ASSOC);
  60. }
  61. function fetch_result($SQL, $result_type = MYSQL_BOTH) {
  62. $arr = array();
  63. $query = $this->query($SQL);
  64. while ($data = mysql_fetch_array($query, $result_type)) {
  65. $arr[] = $data;
  66. }
  67. $this->free_result();
  68. return $arr;
  69. }
  70. function affected_rows() {
  71. return mysql_affected_rows($this->conn);
  72. }
  73. function num_rows($SQL) {
  74. $query = $this->query($SQL);
  75. if (!is_bool($query)) {
  76. return mysql_num_rows($query);
  77. }
  78. return 0;
  79. }
  80. function num_fields($SQL) {
  81. $query = $this->query($SQL);
  82. return mysql_num_fields($query);
  83. }
  84. function escape_string($str) {
  85. return mysql_escape_string($str);
  86. }
  87. function free_result() {
  88. $void = func_get_args();
  89. foreach ($void as $query) {
  90. if (is_resource($query) && get_resource_type($query) === 'mysql result') {
  91. mysql_free_result($query);
  92. }
  93. }
  94. unset($void);
  95. }
  96. function close() {
  97. $this->free_result();
  98. return @mysql_close($this->conn);
  99. }
  100. function halt($msg = null) {
  101. exit($msg . '<br /><br />' . $sql . '<br /> ' . mysql_error());
  102. }
  103. }