一个php调用数据库的类

php中文网
发布: 2016-07-25 09:10:41
原创
1198人浏览过
  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. }

    立即学习PHP免费学习笔记(深入)”;

  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 {
  78. $rs[]=mysql_db_name($rsPtr,$i);
  79. $i++;
  80. }
  81. return $rs;
  82. }
  83. function dbhalt($errmsg){

    库宝AI
    库宝AI

    库宝AI是一款功能多样的智能伙伴助手,涵盖AI写作辅助、智能设计、图像生成、智能对话等多个方面。

    库宝AI 109
    查看详情 库宝AI
  84. $msg="数据库有问题!";
  85. $msg=$errmsg;
  86. echo"$msg";
  87. die();
  88. }
  89. //删

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

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

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

调用示例:

  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. ?>
复制代码


PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号