-
-
class DataBase - {
- var $pConnect=FALSE;//Whether to use long connection
- var $mHost;//Database host
- var $mDatabase;
- var $db ; //Database
- var $mUser; //Database user name
- var $mPwd; // Database user password
- var $mConn; // Connection ID
- var $result; // Resource ID of the result of executing the query command
- var $ num_rows;//The number of entries returned
- var $insert_id;// Returns the ID of the last INSERT command used
- var $affected_rows;// Returns the number of columns affected by the query command
- // Affected by INSERT, UPDATE or DELETE The number of rows.
- // delete without where, then returns 0
- //Constructor function
- public function __construct($host,$user,$pwd,$db)
- {
- $this->mHost=$host;
- $ this->mUser=$user;
- $this->mPwd=$pwd;
- $this->db=$db;
- }
-
- //Database connection
- public function connect()
- {
- if( $this->pConnect)
- $this->mConn=mysql_pconnect($this->mHost,$this->mUser,$this->mPwd);//Long connection
- else
- $this-> ;mConn=mysql_connect($this->mHost,$this->mUser,$this->mPwd);//short connect
if(!$this->mConn) $this->dbhalt("Cannot connect to database!");
- if($this->db=="") $this->db=$this->dbDatabase;
-
- if(!mysql_select_db( $this->db,$this->mConn))
- $this->dbhalt("Database not available!");
- } // eof#dbconnect()
//Change the database
- public function dbChange($db){
- $this->db=$db;
- $this->connect();
- }
//Execute SQL Statement, return result resource id
- public function execute($sql){
- $this->result=mysql_query($sql);
- return $this->result;
- }
//Get array-index and association
- public function fetchArray($resultType=MYSQL_BOTH)
- {
- return mysql_fetch_array($this->result,$resultType);
- }
-
- //Get associative array
- public function fetchAssoc()
- {
- return mysql_fetch_assoc($this->result);
- }
-
- //Get the numeric index array
- public function fetchIndexArray()
- {
- return mysql_fetch_row($this->result);
- }
-
- // Get the object array
- public function fetchObject()
- {
- return mysql_fetch_object($this->result);
- }
-
- //Return the number of record rows
- function numRows()
- {
- return mysql_num_rows($this->result );
- }
//Return all database names in the host
- public function dbNames()
- {
- $rsPtr=mysql_list_dbs($this->mConn);
- $i=0;
- $cnt=mysql_num_rows($rsPtr);
- while($i<$cnt)
- {
- $rs[]=mysql_db_name($rsPtr,$i);
- $i++;
- }
- return $rs;
- }< ;/p>
function dbhalt($errmsg){
- $msg="There is a problem with the database!";
- $msg=$errmsg;
- echo "$msg";
- die();
- }< /p>
//Delete
- function delete($sql){
- $result=$this->execute($sql,$dbbase);
- $this->affected_rows=mysql_affected_rows($this- >dbLink);
- $this->free_result($result);
- return $this->affected_rows;
- }
//Add
- function insert($sql){
- $result=$this->execute($sql,$dbbase);
- $this->insert_id=mysql_insert_id($this->dbLink);
- $this->free_result($result);
- return $ this->insert_id;
- }
//Change
- function update($sql){
- $result=$this->execute($sql,$dbbase);
- $this ->affected_rows=mysql_affected_rows($this->dbLink);
- $this->free_result($result);
- return $this->affected_rows;
- }
-
- //Close the connection
- function dbclose(){
- mysql_close($this->dbLink);
- }
- }// end class
- ?>
-
Copy code
Call example:
-
-
- 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());
- ?>
-
Copy code
|