Mysql
/*************************************
MySQL connection class implementation-write by 3,000
**************************************/
/*
Use method (you can perform general MySQL commands, insert, delete select Update)
Before the file, you need to load the method
Require ("mysql.class.php")
$db =new Dbmysql; Load class
$db->dbserver= "localhost";
$db->dbuser= "root";
$db->dbpwd= "";
$db->dbdatabase= "";
$db->dbconnect ();Query ($sql, $database);
Query_first ($sql, $database);
Fetch_array ($sql, $database);
Count_records ($table, $index, $where, $database);)//To get the number of records for a table, $table for the table name, $index for key, $where as a condition, $dbbase as a database, The latter two can not choose
*/
Class Db_mysql
{
var $usepconnect;
var $dbSever;
var $dbDatabase;
var $dbbase;
var $dbUser;
var $dbPwd;
var $dbLink;
var $query _id;//A pointer to execute the query command
Number of entries returned by Var $num _rows;//
var $insert _id;//Returns the ID of the last time the insert instruction was used
var $affected _rows;//Returns the number of columns affected by the query command
The number of columns (row) affected by INSERT, UPDATE, or DELETE.
Delete returns 0 if no where is taken
function Dbconnect ($dbbase, $usepconnect)
{
Global $usepconnect;
if ($usepconnect ==1)
$this->dblink= @mysql_pconnect ($this->dbserver, $this->dbuser, $this->dbpwd);//long Connect
Else
$this->dblink= @mysql_connect ($this->dbserver, $this->dbuser, $this->dbpwd);//short Connect
$this->dbhalt ($this->dbbase);
if (! $this->dblink) $this->dbhalt ("exsiting error when connecting!");
if ($this->dbbase== "") $this->dbbase= $this->dbdatabase;
if (dbbase, $this->dblink ">! @mysql_select_db ($this->dbbase, $this->dblink))
$this->dbhalt ("can ' t use this database,please check database!");
}//End func () Connect database
Change Database
function dbchange_db ($dbbase) {
$this->dbconnect ($dbbase);
}
function Dbquery ($sql) {
if ($dbbase!= "") $this->dbchange_db ($dbbase);
$this->query_id=mysql_query ($sql);
Print ($query _id);
if (! $this->query_id) $this->dbhalt ("Wrong SQL sentence!". $SQL);
return $this->query_id;
}
function Dbquery_first ($sql, $dbbase) {
$query _id=dbquery ($sql, $dbbase);
$returnarray =mysql_fetch_array ($query _id);
$this->num_rows=mysql_num_rows ($query _id);
$this->dbfree_result ($query _id);
return $returnarray;
}
function Dbhalt ($errmsg) {
$msg = "
Database is wrong!
";
$msg = $errmsg;
echo "$msg";
Die ();
}
function Dbfetch_array ($sql, $dbbase, $type) {
$query _id= $this->dbquery ($sql, $dbbase);
$this->numrows=mysql_num_rows ($query _id);
For ($i =0 $i < $this->numrows; $i + +) {
if ($type ==0)
$array [$i]=mysql_fetch_array ($query _id);
Else
$array [$i]=mysql_fetch_array ($query _id);
}
$this->dbfree_result ($query _id);
return $array;
}
function Dbdelete ($sql, $dbdase) {
$query _id= $this->dbquery ($sql, $dbbase);
$this->affected_rows=mysql_affected_rows ($this->dblink);
$this->free_reuslt ($query _id);
}
function Dbinsert ($sql, $dbbase) {
$query _id= $this->dbquery ($sql, $dbbase);
$this->insert_id=mysql_insert_id ($this->dblink);
$this->affected_rows=mysql_affected_rows ($this->dblink);
$this->free_reuslt ($query _id);
}
function Dbupdate ($sql, $dbbase) {
$query _id= $this->dbquery ($sql, $dbbase);
$this->insert_id=mysql_insert_id ($this->dblink);
$this->affected_rows=mysql_affected_rows ($this->dblink);
$this->free_reuslt ($query _id);
}
function Dbcount_records ($table, $index = "id", $where = "", $dbbase) {
if ($dbbase!= "") $this->dbchangedb ($dbbase);
$result = @mysql_query ("SELECT count (". $index. ") As ' num ' form '. $table. " where ". $where, $this->dblink);
if (! $result) $this->dbhalt ("wrong SQL sentence". $sql);
$num = @mysql_result ($result, 0, "num");
return $num;
}
function Dbgetnum ($result) {
$num = @mysql_numrows ($result);
return $num;
}
function Dbfree_result ($result _id) {
@mysql_free_result ($query _id);
}
function Dbclose () {
Mysql_close ($this->dblink);
}
}//End Class
$db =new Db_mysql;
$db->dbserver= "localhost";
$db->dbuser= "root";
$db->dbpwd= "";
$db->dbbase= "Test";
?>