Many friends of the newly-learned PHP may be a bit troublesome in the database, especially after mysql4.1.x will appear garbled problem. Here is a simple tutorial, hoping to help a little beginner. Perhaps a lot of friends before is to learn ASP (I also), may miss the ASP's set Rs=adodb.recorset (I, too long did not do ASP, the back seems a bit wrong, can not think of it! Gather around and watch! ) and then Rs.open,rs.movenext ..... But PHPA relies on a lot of database operation functions to control, for example: mysql_connect (); mysql_select_db (); If there are more pages, do you want to write these functions repeatedly??? Of course not, now give everyone a database operation class, which contains most of the database operation methods, including basic configuration information, then we need to call the database information directly include this page can be, the following code and use methods.
First you need two page 1.config.inc.php code:
Copy Code code as follows:
<?php
Database parameter variable setting
$dbhost: Host Name
$dbuser: Connection Account
$dbpassword: Connection Password
$dbname: Database name
I have configured the following as an example,
Please configure this file according to your own database configuration information.
//--------------------------------------------------------------------
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "7529639";
$dbname = "Cr_download";
//--------------------------------------------------------------------
?>
The other is the database Operation class page dbclass.php
Copy Code code as follows:
<?php
Defining Database Operations Classes
Class db{
Class attribute definition
var $dbhost = "localhost";//mysql host
var $dbuser = "root";//Connection account
var $password = "";//Connection password
var $dbname = "";//Database name
Variable reference
function MySQL ($dbhost, $dbuser, $password, $dbname) {
$this->dbhost= $dbhost;
$this->dbuser= $dbuser;
$this->password= $password;
$this->dbname= $dbname;
}
Create a MySQL connection
function Mycon () {
@mysql_connect ($this->dbhost, $this->dbuser, $this->password);
}
Select the appropriate database
function Selectdb () {
@mysql_select_db ($this->db);
}
Create a database connection and select the appropriate database
function Createcon () {
Mysql_connect ($this->dbhost, $this->dbuser, $this->password);
mysql_query ("SET NAMES ' GBK '");//This is the key to solve the garbled, Linux changed to UTF8
mysql_select_db ($this->dbname);
}
Executes the SQL statement and returns the result set
function Fetch_array ($sql) {
$result =mysql_query ($sql);
return mysql_fetch_array ($result);
}
Execute SQL statement
function query ($sql) {
return mysql_query ($sql);
}
Get an array of result sets
function Loop_query ($result) {
return mysql_fetch_array ($result);
}
To close a database connection
function Close () {
return Mysql_close ();
}
}
?>
The following usage:
If a page involves database operations, use this:
Copy Code code as follows:
<?php
Include (' inc/config.inc.php ');//contains database basic configuration information
Include (' inc/dbclass.php ');//contains Database operations class
The following is an example of inserting a piece of data to illustrate that other operational usages are similar
//-----------------------------------------------------------------------------------
$db =new db;//to generate an instance from a database operation class, OOP is still good.
$db->mysql ($dbhost, $dbuser, $dbpassword, $dbname);//Call connection parameter function
$db->createcon ()//call to create a join function
//-----------------------------------------------------------------------------------
Start inserting data
//-----------------------------------------------------------------------------------
$addsql = "INSERT into Cr_userinfo values (0, ' $username ', ' $userpwd ', ' $time ', 50,1, ' $userquestion ', ' $useranswer ')";
$db->query ($addsql);
echo " <font color=red> Congratulations, register successfully! Please click <a href=login.php> here </a> login! </font> ";
$db->close ();//Close database connection
?>
Well, after reading this article believe that the Novice can be very good use of PHP to the MySQL basic data add, delete and other operations, and code specifications, easy to maintain. I wish you a happy study, there is no understanding of the reply to the message, I will be the first time to reply to ^_^.