Many friends of the newly-learned PHP may be a bit of a problem in the database, especially after the mysql4.1.x will appear garbled problem. Here is a simple tutorial, hoping to help the novice a little. Perhaps a lot of friends before is learning ASP (I also), may miss the ASP set Rs=adodb.recorset (I, too long did not do ASP, the back seems a bit wrong, can't think up!) Stay alive and watch! And then just rs.open,rs.movenext ..... However, PHPA is controlled by a number of database manipulation functions, such as: mysql_connect (); mysql_select_db (); If there are many pages, do you want to repeat these functions??? Of course not, now give everyone a database operation class, which contains most of the methods of database operations, including basic configuration information, in the future we need to call the database information directly include this page can be, the following code and how to use.
First, you need two pages 1.config.inc.php code:
Copy CodeThe code is as follows:
Database parameter variable setting
$dbhost: Host Name
$dbuser: Connection Account
$dbpassword: Connection Password
$dbname: Database name
Here is my machine configuration as an example,
Please configure this file according to your database configuration information.
//--------------------------------------------------------------------
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "7529639";
$dbname = "Cr_download";
//--------------------------------------------------------------------
?> another is the database Operation class page dbclass.php
Copy CodeThe code is as follows: Defining database Operation Classes
Class db{
Class Property Definition
var $dbhost = "localhost";//mysql host
var $dbuser = "root";//Connect 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 solving garbled characters Oh, 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 ();
}
}
?>
Here's how to use:
If a page involves database operations, use this:
Copy CodeThe code is as follows:
Include (' inc/config.inc.php ');//contains database basic configuration information
Include (' inc/dbclass.php ');//contains database operation class
The following is an example of inserting a piece of data, similar to other operational usages
//-----------------------------------------------------------------------------------
$db =new db;//generate an instance from the database operation class, OOP is good.
$db->mysql ($dbhost, $dbuser, $dbpassword, $dbname);//Call the connection parameter function
$db->createcon ();//call to create a connection function
//-----------------------------------------------------------------------------------
Start inserting data
//-----------------------------------------------------------------------------------
$addsql = "INSERT into Cr_userinfo values (0, ' $username ', ' $userpwd ', ' $time ', 50,1, ' $userquestion ', ' $useranswer ')";
$db->query ($addsql);
echo "Congratulations, the registration is successful!" Please click here to login! ";
$db->close ();//Close database connection
?>
Well, after reading this article, I believe that beginners can use PHP to make Basic data additions, deletions and so on, and code specification, easy to maintain. I wish you a happy study, have not understand the reply message, I will be the first time to reply to ^_^.
http://www.bkjia.com/PHPjc/317715.html www.bkjia.com true http://www.bkjia.com/PHPjc/317715.html techarticle Many friends of the newly-learned PHP may be a bit of a problem in the database, especially after the mysql4.1.x will appear garbled problem. Here is a simple tutorial, hoping to help the novice a little. Maybe a lot of friends ...