Copy CodeThe code is as follows:
Class dbclass{//Start database classes
var $username;
var $password;
var $database;
var $hostname;
var $link;
var $result;
function Dbclass ($username, $password, $database, $hostname = "localhost") {
$this->username= $username;
$this->password= $password;
$this->database= $database;
$this->hostname= $hostname;
}
function connect () {//This is used to connect to the database
if (! $this->link=mysql_connect ($this->hostname, $this->username, $this->password))
$this->halt ("Sorry,can not connect to database");
if ($this->version () > ' 4.1 ') {
Global $dbcharset, $charset;
if (! $dbcharset && In_array (Strtolower ($charset), Array (' GBK ', ' Big5 ', ' utf-8 ')) {
$dbcharset = Str_replace ('-', ' ', $charset);
}
if ($dbcharset) {
mysql_query ("SET character_set_c character_set_results= $dbcharset, character_set_client=binary");
}
}
if ($this->version () > ' 5.0.1 ') {
mysql_query ("SET sql_mode=");
}
return $this->link;
}
function Select () {//This is used to select the database
mysql_select_db ($this->database, $this->link);
}
Functions Query ($SQL) {//This function is used to send out query statements and return results, commonly used.
if ($this->result=mysql_query ($sql, $this->link)) return $this->result;
else {
$this->halt ("SQL statement error: $sql
Error message: ". Mysql_error ());
return false;
}
}
/*
The following function is used to retrieve an array from the result, usually in conjunction with the while () loop, $db->query ($sql), for example:
$result =query ("SELECT * from MyTable");
while ($row = $db->getarray ($result)) {
echo "$row [id]";
}
*/
function GetArray ($result) {
Return @mysql_fetch_array ($result);
}
/*
The following function is used to get the first row of the SQL query and is typically used to query for the existence of qualifying rows, for example:
The user name $username, password $password that the user submits from the form are in the user table "user" and returns its corresponding array:
if ($user = $db->getfirst ("select * from user where username= ' $username ' and password= ' $password '"))
echo "Welcome $username, your ID is $user [id]. ";
Else
echo "username or password Error! ";
*/
function GetFirst ($sql) {
Return @mysql_fetch_array ($this->query ($sql));
}
/*
The following function returns the total number of rows that meet the query criteria, such as calculations for paging, for example:
$totlerows = $db->getcount ("SELECT * from MyTable");
echo "Total $totlerows information. ";
*/
function GetCount ($sql) {
Return @mysql_num_rows ($this->query ($sql));
}
/*
The following functions are used to update a database, such as a user change password:
$db->update ("Update user set password= ' $new _password ' where userid= ' $userid '");
*/
function Update ($sql) {
return $this->query ($sql);
}
/*
The following function is used to insert a row into the database, such as adding a user:
$db->insert ("INSERT into User (Userid,username,password) VALUES (null, ' $username ', ' $password ')");
*/
function Insert ($sql) {
return $this->query ($sql);
}
function GetID () {//This is used to get the ID of the row just inserted
return mysql_insert_id ();
}
function Num_rows ($query) {
$query = mysql_num_rows ($query);
return $query;
}
function Num_fields ($query) {
Return Mysql_num_fields ($query);
}
function Free_result ($query) {
Return Mysql_free_result ($query);
}
Function version () {
return Mysql_get_server_info ();
}
function Close () {
return Mysql_close ();
}
function Halt ($message = ") {
return $message;
}
}
$db =new dbclass ("$db _username", "$db _password", "$db _database", "$db _hostname");
$db->connect ();
$db->select ();
The above describes the ManagementClass PHP call MySQL data Dbclass class, including the managementclass aspect of the content, I hope the PHP tutorial interested in a friend to help.