php数据库类操作实例之读取数据,PHP数据库类及操作实例源代码

PHP数据库类及操作实例,虽然不是很强大,但是用起来还是蛮方便的

db.class.php

PHP代码

01.<?php

02./**

03.*数据库类

04.*/

05.class DataBase

06.{

07. var $pConnect=FALSE;//是否使用长连接

08. var $mHost;//数据库主机

09. var $mDatabase;

10. var $db; //数据库

11. var $mUser;//数据库用户名

12. var $mPwd;//数据库用户密码

13. var $mConn;//连接标识

14. var $result;// 执行query命令的结果资源标识

15. var $num_rows;// 返回的条目数

16. var $insert_id;// 传回最后一次使用 INSERT 指令的 ID

17. var $affected_rows;// 传回query命令所影响的列数目

18. // INSERT、UPDATE 或 DELETE 所影响的列 (row) 数目。

19. // delete 如果不带where,那么则返回0

20. //构造函数

21. public function __construct($host,$user,$pwd,$db)

22. {

23. $this->mHost=$host;

24. $this->mUser=$user;

25. $this->mPwd=$pwd;

26. $this->db=$db;

27. }

28.

29. //数据库连接

30. public function connect()

31. {

32. if($this->pConnect)

33. $this->mConn=mysql_pconnect($this->mHost,$this->mUser,$this->mPwd);//长连接

34. else

35. $this->mConn=mysql_connect($this->mHost,$this->mUser,$this->mPwd);//short connect

36.

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

38. if($this->db=="") $this->db=$this->dbDatabase;

39.

40. if(!mysql_select_db($this->db,$this->mConn))

41. $this->dbhalt("数据库不可用!");

42. } // eof#dbconnect()

43.

44. //更改数据库

45. public function dbChange($db){

46. $this->db=$db;

47. $this->connect();

48. }

49.

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

51. public function execute($sql){

52. $this->result=mysql_query($sql);

53. return $this->result;

54. }

55.

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

57. public function fetchArray($resultType=MYSQL_BOTH)

58. {

59. return mysql_fetch_array($this->result,$resultType);

60. }

61.

62. //获取关联数组

63. public function fetchAssoc()

64. {

65. return mysql_fetch_assoc($this->result);

66. }

67.

68. //获取数字索引数组

69. public function fetchIndexArray()

70. {

71. return mysql_fetch_row($this->result);

72. }

73.

74. //获取对象数组

75. public function fetchObject()

76. {

77. return mysql_fetch_object($this->result);

78. }

79.

80. //返回记录行数

81. function numRows()

82. {

83. return mysql_num_rows($this->result);

84. }

85.

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

87. public function dbNames()

88. {

89. $rsPtr=mysql_list_dbs($this->mConn);

90. $i=0;

91. $cnt=mysql_num_rows($rsPtr);

92. while($i

93. {

94. $rs[]=mysql_db_name($rsPtr,$i);

95. $i++;

96. }

97. return $rs;

98. }

99.

100. function dbhalt($errmsg){

101. $msg="数据库有问题!";

102. $msg=$errmsg;

103. echo"$msg";

104. die();

105. }

106.

107. //删

108. function delete($sql){

109. $result=$this->execute($sql,$dbbase);

110. $this->affected_rows=mysql_affected_rows($this->dbLink);

111. $this->free_result($result);

112. return $this->affected_rows;

113. }

114.

115. //增

116. function insert($sql){

117. $result=$this->execute($sql,$dbbase);

118. $this->insert_id=mysql_insert_id($this->dbLink);

119. $this->free_result($result);

120. return $this->insert_id;

121. }

122.

123. //改

124. function update($sql){

125. $result=$this->execute($sql,$dbbase);

126. $this->affected_rows=mysql_affected_rows($this->dbLink);

127. $this->free_result($result);

128. return $this->affected_rows;

129. }

130.

131. //关闭连接

132. function dbclose(){

133. mysql_close($this->dbLink);

134. }

135.}// end class

136.?>

直接引用即可:

PHP代码

01.<?php

02.include "db.class.php";

03.

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

05.$mydb->connect();

06.$mydb->execute("set names GBK");

07.$mydb->execute("select * from usrs");

08.print_r($mydb->dbNames());

09.?>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值