As the saying goes, "good start is half of success", and Php+mysql Project database operation is one of the key, can simplify the compilation of database operating procedures, become a key to the impact of work efficiency.
So sunny little is not the first to do the page, but first set up a "dbclass.php" file, began to write operations MySQL database class "Dbclass". The following program is written in "dbclass.php":
$db _username= "MyUserName"; User name to connect to database
$db _password= "MyPassword"; Password to connect to the database
$db _database= "MyDatabase"; Database name
$db _hostname= "localhost"; Server address
Class dbclass{//Start Database class
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;
}
Functions Connect () {//This function is used to connect to the database
$this->link=mysql_connect ($this->hostname, $this->username, $this->password) or Die ("Sorry,can not Connect to Database ");
return $this->link;
}
function Select () {///It is used to choose a database
mysql_select_db ($this->database, $this->link);
}
The function query ($sql) {//is used to send out query statements and return results.
if ($this->result=mysql_query ($sql, $this->link)) return $this->result;
else {
Here is the error message that shows the SQL statement, primarily the design phase for hints. The following sentence can be commented out during the formal run phase.
echo "SQL statement error: $sql
Error message: ". Mysql_error ();
return false;
}
}
/*
The following functions are used to retrieve an array from the result, typically 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, which is generally used to query whether a qualifying row exists, for example:
User name $username, password $password submitted from form to user, and return the corresponding array:
if ($user = $db->getfirst ("select * from user where username= ' $username ' and Password= '"))
echo "Welcome $username, your ID is $user [id]. ";
Else
echo "wrong username or password!";
*/
function GetFirst ($sql) {
Return @mysql_fetch_array ($this->query ($sql));
}
/*
The following function returns the total number of rows that match the query criteria, such as calculations for paging, for example:
$totlerows = $db->getcount ("SELECT * from MyTable");
echo "has $totlerows piece of information. ";
*/
function GetCount ($sql) {
Return @mysql_num_rows ($this->query ($sql));
}
/*
The following functions are used to update a database, such as a user changing a 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 () {//) to get the ID of the row just inserted
return mysql_insert_id ();
}
}
/*
The main function is these, if you have another need, you can add it yourself.
Because any use of this class must be connected to the database, the following link and select a good database:
*/
$db =new dbclass ("$db _username", "$db _password", "$db _database", "$db _hostname");
$db->connect ();
$db->select ();
?>
OK, the database class has been written, it can not only be used in the current project, other projects of the same application! Just copy the "dbclass.php" to the past. When you want to use this file, just use the statement "include_once (" dbclass.php ")" On the line, the specific syntax in writing database classes have been examples, no longer repeat.
After the class of the database is written, the operation of the database is much more convenient, and the production of the project has taken an important first step.