mysql数据库连接类

本款php连接mysql数据库连接程序代码是一款比较简单实用的连接代码,希望本教程对各位同学会有所帮助,代码如下:

  1. class mysql {
  2. public $sqlserver = 'localhost';
  3. public $sqluser = 'root';
  4. public $sqlpassword = '';
  5. public $database;
  6. public $last_query = '';
  7. private $connection;
  8. private $query_result;
  9. public function __construct() {}
  10. public function __destruct() {
  11. $this->close();
  12. }
  13. //+======================================================+
  14. // create a connection to the mysql database
  15. //+======================================================+
  16. public function connect($server = null, $user = null, $password = null, $database = null){
  17. if (isset($server)) $this->sqlserver = $server;
  18. if (isset($user)) $this->sqluser = $user;
  19. if (isset($password)) $this->sqlpassword = $password;
  20. if (isset($database)) $this->database = $database;
  21. $this->connection = mysql_connect($this->sqlserver, $this->sqluser, $this->sqlpassword);
  22. if($this->connection){
  23. if (mysql_select_db($this->database)){
  24. return $this->connection;
  25. }else{
  26. return $this->error();
  27. }
  28. }else{
  29. return $this->error();
  30. }
  31. }
  32. //+======================================================+
  33. // execute a query
  34. //+======================================================+
  35. public function query($query, $die = false){
  36. if ($query != null){
  37. $this->last_query = $query;
  38. $this->query_result = mysql_query($query, $this->connection);
  39. if(!$this->query_result){
  40. if ($die) die("die: ".$this->query_result);
  41. return $this->error();
  42. }else{
  43. if ($die) die("die: ".$this->query_result);
  44. return $this->query_result;
  45. }
  46. }else{
  47. echo "empty query cannot be executed!";
  48. }
  49. }
  50. //+======================================================+
  51. // returns the result
  52. //+======================================================+
  53. public function getresult(){
  54. return $this->query_result;
  55. }
  56. //+======================================================+
  57. // returns the connection
  58. //+======================================================+
  59. public function getconnection(){
  60. return $this->connection;
  61. }
  62. //+======================================================+
  63. // returns an object with properties rep
  64. // resenting the result fields www.111cn.net and values
  65. //+======================================================+
  66. public function getobject($qry = null){
  67. if (isset($qry)) $this->query($qry);
  68. return mysql_fetch_object($this->getresult());
  69. }
  70. //+======================================================+
  71. // returns an array with keys representi
  72. // ng the result fields and values
  73. //+======================================================+
  74. public function getarray($query_id = ""){
  75. if($query_id == null){
  76. $return = mysql_fetch_array($this->getresult());
  77. }else{
  78. $return = mysql_fetch_array($query_id);
  79. }
  80. return $return ? $return : $this->error();
  81. }
  82. //+======================================================+
  83. // returns the number of rows in the res
  84. // ult
  85. //+======================================================+
  86. public function getnumrows($qry = null){
  87. if (isset($qry)) $this->query($qry);
  88. $amount = mysql_num_rows($this->getresult());
  89. return emptyempty($amount) ? 0 : $amount;
  90. }
  91. //+======================================================+
  92. // returns if the result contains rows
  93. //+======================================================+
  94. public function hasresults($qry = null) {
  95. if (isset($qry)) $this->query($qry);
  96. return $this->getnumrows($qry) > 0;
  97. }
  98. //+======================================================+
  99. // returns the number of rows that where
  100. // affected by the last action
  101. //+======================================================+
  102. public function getaffectedrows($qry = null, $query_id = null){
  103. if (isset($qry)) $this->query($qry);
  104. if(emptyempty($query_id)){
  105. $return = mysql_affected_rows($this->getresult());
  106. }else{
  107. $return = mysql_affected_rows($query_id);
  108. }
  109. return $return ? $return : $this->error();
  110. }
  111. //+======================================================+
  112. // returns the auto generated id from th
  113. // e last insert action
  114. //+======================================================+
  115. public function getinsertid($connection_link = null){
  116. return mysql_insert_id(isset($connection_link) ? $connection_link : $this->connection);
  117. }
  118. //+======================================================+
  119. // close the connection to the mysql dat
  120. // abase
  121. //+======================================================+
  122. public function close(){
  123. if(isset($this->connection)){
  124. return @mysql_close($this->connection);
  125. }
  126. else {
  127. return $this->error();
  128. }
  129. }
  130. //+======================================================+
  131. // outputs the mysql error
  132. //+======================================================+
  133. private function error(){
  134. if(mysql_error() != ''){
  135. echo '<b>mysql errorwww.111cn.net</b>: '.mysql_error().'<br/>';
  136. }
  137. }
  138. }
  139. //demo
  140. // database object initialization
  141. $db = new mysql();
  142. $db->connect("localhost", "root", "123456", "user");
  143. // update query
  144. //$db->query("update table_name set field_name = value where another_field = another_value");
  145. // select with check for record amount
  146. if ($db->hasresults("select * from userinfo")) {
  147. // loop through the user records, and get them as objects
  148. // note that the getobject method will use the last executed query when not provided with a new one
  149. while ($user = $db->getobject()) {
  150. echo "user $user->username is called $user->password<br /> ";
  151. }//开源代码phpfensi.com
  152. }
  153. else {
  154. echo "no results where found";
  155. }