-
-
class DataBase
- {
- var $pConnect=FALSE;//是否使用长连接
- var $mHost;//数据库主机
- var $mDatabase;
- var $db; //数据库
- var $mUser;//数据库用户名
- var $mPwd;//数据库用户密码
- var $mConn;//连接标识
- var $result;// 执行query命令的结果资源标识
- var $num_rows;// 返回的条目数
- var $insert_id;// 传回最后一次使用 INSERT 指令的 ID
- var $affected_rows;// 传回query命令所影响的列数目
- // INSERT、UPDATE 或 DELETE 所影响的列 (row) 数目。
- // delete 如果不带where,那么则返回0
- //构造函数
- public function __construct($host,$user,$pwd,$db)
- {
- $this->mHost=$host;
- $this->mUser=$user;
- $this->mPwd=$pwd;
- $this->db=$db;
- }
-
- //数据库连接
- public function connect()
- {
- if($this->pConnect)
- $this->mConn=mysql_pconnect($this->mHost,$this->mUser,$this->mPwd);//长连接
- else
- $this->mConn=mysql_connect($this->mHost,$this->mUser,$this->mPwd);//short connect
if(!$this->mConn) $this->dbhalt("不能连接数据库!");
- if($this->db=="") $this->db=$this->dbDatabase;
-
- if(!mysql_select_db($this->db,$this->mConn))
- $this->dbhalt("数据库不可用!");
- } // eof#dbconnect()
//更改数据库
- public function dbChange($db){
- $this->db=$db;
- $this->connect();
- }
//执行SQL语句,返回结果资源id
- public function execute($sql){
- $this->result=mysql_query($sql);
- return $this->result;
- }
//获取数组-索引和关联
- public function fetchArray($resultType=MYSQL_BOTH)
- {
- return mysql_fetch_array($this->result,$resultType);
- }
-
- //获取关联数组
- public function fetchAssoc()
- {
- return mysql_fetch_assoc($this->result);
- }
-
- //获取数字索引数组
- public function fetchIndexArray()
- {
- return mysql_fetch_row($this->result);
- }
-
- //获取对象数组
- public function fetchObject()
- {
- return mysql_fetch_object($this->result);
- }
-
- //返回记录行数
- function numRows()
- {
- return mysql_num_rows($this->result);
- }
//返回主机中所有数据库名
- public function dbNames()
- {
- $rsPtr=mysql_list_dbs($this->mConn);
- $i=0;
- $cnt=mysql_num_rows($rsPtr);
- while($i {
- $rs[]=mysql_db_name($rsPtr,$i);
- $i++;
- }
- return $rs;
- }
function dbhalt($errmsg){
- $msg="数据库有问题!";
- $msg=$errmsg;
- echo"$msg";
- die();
- }
//删
- function delete($sql){
- $result=$this->execute($sql,$dbbase);
- $this->affected_rows=mysql_affected_rows($this->dbLink);
- $this->free_result($result);
- return $this->affected_rows;
- }
//增
- function insert($sql){
- $result=$this->execute($sql,$dbbase);
- $this->insert_id=mysql_insert_id($this->dbLink);
- $this->free_result($result);
- return $this->insert_id;
- }
//改
- function update($sql){
- $result=$this->execute($sql,$dbbase);
- $this->affected_rows=mysql_affected_rows($this->dbLink);
- $this->free_result($result);
- return $this->affected_rows;
- }
-
- //关闭连接
- function dbclose(){
- mysql_close($this->dbLink);
- }
- }// end class
- ?>
-
复制代码
调用示例:
-
-
include "class_database.php";
$mydb=new DataBase("localhost","root","123456","test");
- $mydb->connect();
- $mydb->execute("set names GBK");
- $mydb->execute("select * from usrs");
- print_r($mydb->dbNames());
- ?>
-
复制代码
|
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31