当前位置:Gxlcms > PHP教程 > 一个php调用数据库的类

一个php调用数据库的类

时间:2021-07-01 10:21:17 帮助过:3人阅读

  1. class DataBase

  2. {
  3. var $pConnect=FALSE;//是否使用长连接
  4. var $mHost;//数据库主机
  5. var $mDatabase;
  6. var $db; //数据库
  7. var $mUser;//数据库用户名
  8. var $mPwd;//数据库用户密码
  9. var $mConn;//连接标识
  10. var $result;// 执行query命令的结果资源标识
  11. var $num_rows;// 返回的条目数
  12. var $insert_id;// 传回最后一次使用 INSERT 指令的 ID
  13. var $affected_rows;// 传回query命令所影响的列数目
  14. // INSERT、UPDATE 或 DELETE 所影响的列 (row) 数目。
  15. // delete 如果不带where,那么则返回0
  16. //构造函数
  17. public function __construct($host,$user,$pwd,$db)
  18. {
  19. $this->mHost=$host;
  20. $this->mUser=$user;
  21. $this->mPwd=$pwd;
  22. $this->db=$db;
  23. }
  24. //数据库连接
  25. public function connect()
  26. {
  27. if($this->pConnect)
  28. $this->mConn=mysql_pconnect($this->mHost,$this->mUser,$this->mPwd);//长连接
  29. else
  30. $this->mConn=mysql_connect($this->mHost,$this->mUser,$this->mPwd);//short connect

  31. if(!$this->mConn) $this->dbhalt("不能连接数据库!");

  32. if($this->db=="") $this->db=$this->dbDatabase;
  33. if(!mysql_select_db($this->db,$this->mConn))
  34. $this->dbhalt("数据库不可用!");
  35. } // eof#dbconnect()

  36. //更改数据库

  37. public function dbChange($db){
  38. $this->db=$db;
  39. $this->connect();
  40. }

  41. //执行SQL语句,返回结果资源id

  42. public function execute($sql){
  43. $this->result=mysql_query($sql);
  44. return $this->result;
  45. }

  46. //获取数组-索引和关联

  47. public function fetchArray($resultType=MYSQL_BOTH)
  48. {
  49. return mysql_fetch_array($this->result,$resultType);
  50. }
  51. //获取关联数组
  52. public function fetchAssoc()
  53. {
  54. return mysql_fetch_assoc($this->result);
  55. }
  56. //获取数字索引数组
  57. public function fetchIndexArray()
  58. {
  59. return mysql_fetch_row($this->result);
  60. }
  61. //获取对象数组
  62. public function fetchObject()
  63. {
  64. return mysql_fetch_object($this->result);
  65. }
  66. //返回记录行数
  67. function numRows()
  68. {
  69. return mysql_num_rows($this->result);
  70. }

  71. //返回主机中所有数据库名

  72. public function dbNames()
  73. {
  74. $rsPtr=mysql_list_dbs($this->mConn);
  75. $i=0;
  76. $cnt=mysql_num_rows($rsPtr);
  77. while($i<$cnt)
  78. {
  79. $rs[]=mysql_db_name($rsPtr,$i);
  80. $i++;
  81. }
  82. return $rs;
  83. }

  84. function dbhalt($errmsg){

  85. $msg="数据库有问题!";
  86. $msg=$errmsg;
  87. echo"$msg";
  88. die();
  89. }

  90. //删

  91. function delete($sql){
  92. $result=$this->execute($sql,$dbbase);
  93. $this->affected_rows=mysql_affected_rows($this->dbLink);
  94. $this->free_result($result);
  95. return $this->affected_rows;
  96. }

  97. //增

  98. function insert($sql){
  99. $result=$this->execute($sql,$dbbase);
  100. $this->insert_id=mysql_insert_id($this->dbLink);
  101. $this->free_result($result);
  102. return $this->insert_id;
  103. }

  104. //改

  105. function update($sql){
  106. $result=$this->execute($sql,$dbbase);
  107. $this->affected_rows=mysql_affected_rows($this->dbLink);
  108. $this->free_result($result);
  109. return $this->affected_rows;
  110. }
  111. //关闭连接
  112. function dbclose(){
  113. mysql_close($this->dbLink);
  114. }
  115. }// end class
  116. ?>

调用示例:

  1. include "class_database.php";

  2. $mydb=new DataBase("localhost","root","123456","test");

  3. $mydb->connect();
  4. $mydb->execute("set names GBK");
  5. $mydb->execute("select * from usrs");
  6. print_r($mydb->dbNames());
  7. ?>

人气教程排行