Home > Backend Development > PHP Tutorial > Detailed explanation of database operations and garbled code resolution for beginners learning PHP! _PHP Tutorial

Detailed explanation of database operations and garbled code resolution for beginners learning PHP! _PHP Tutorial

WBOY
Release: 2016-07-21 15:57:58
Original
877 people have browsed it

Many friends who are new to PHP may have some trouble with the database, especially after MYSQL4.1.x, garbled characters will appear. Here is a simple tutorial, I hope it will be helpful to novices. Maybe many of my friends studied ASP before (so did I), and they may miss ASP’s set rs=adodb.recorset (Damn, I haven’t done ASP for a long time, and the latter part seems a bit wrong, and I can’t remember it! Just watch it while you’re alive! ) and then rs.open, rs.movenext... But PHPA relies on many database operation functions to control, such as: mysql_connect(); mysql_select_db();... ...If there are many pages, do we need to write these functions repeatedly one by one? ? ? Of course not, now I give you a database operation class, which contains most of the database operation methods, including basic configuration information. In the future, if you need to call database information, you can directly include this page. The code and usage are given below.
First you need two pages 1.config.inc.php code:

Copy the code The code is as follows:

//Database parameter variable setting
//$dbhost: host name
//$dbuser: connection account
//$dbpassword: connection password
//$dbname :Database name
//The following is the configuration of my machine 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 page dbclass.php
Copy code The code is as follows:
//Define database operation class
class db{
       // Class attribute definition                                                                                                            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;
host,$ this->dbuser,$this->password);
}
//Select the corresponding database
function selectdb(){
@mysql_select_db($this->db);
}

//Create a database connection and select the corresponding database
function createcon(){
mysql_connect($this->dbhost,$this->dbuser,$this-> password);
mysql_query("SET NAMES 'GBK'");//This is the key to solving garbled characters, change it to UTF8 under LINUX
mysql_select_db($this->dbname);
}
//Execute the SQL statement and return 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 the result set array
function loop_query($result){
        return mysql_fetch_array($result); 🎜> }
?> ;

The following is the usage:
If a page involves database operations, please use it like this:



Copy code

The code is as follows:

include('inc/config.inc.php');//Contains basic database configuration information
include('inc/dbclass.php');//Contains Database operation class
//The following takes inserting a piece of data as an example. The usage of other operations is similar
//------------------------ -------------------------------------------------- ---------
$db=new db;//Generating instances from database operation classes, OOP is still good
$db->mysql($dbhost,$dbuser,$dbpassword, $dbname);//Call the connection parameter function
$db->createcon();//Call the create connection function
//---------------- -------------------------------------------------- ----------------
//Start inserting data
//------------------------ -------------------------------------------------- ------------
$addsql="insert into cr_userinfo values(0,'$username','$userpwd','$time',50,1,'$userquestion', '$useranswer')";
$db->query($addsql);
echo" Congratulations, your registration is successful! Please clickhereLog in! ";
$db->close();//Close the database connection

? >

Okay, after reading this article, I believe that novices can use PHP to perform basic data addition, deletion and other operations on MYSQL. The code is standardized and easy to maintain. I wish you all a happy study. If you have any questions, please leave a message and I will reply as soon as possible ^_^.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317715.htmlTechArticleMany friends who are new to PHP may have some trouble with the database, especially after MYSQL4.1.x, garbled characters will appear. question. Here is a simple tutorial, I hope it will be helpful to novices. Maybe many friends...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template