Php was a beginner, so it was severely frustrated at the beginning in SQL (specifically mysql. So write down some methods here. The first is the basic method. The basic method is not used now, so I will attach the database connection operation of some basic methods in hustoj: 1if ($ OJ_SAE) {forsae.sina.com. cn2mysql_connect (SAE _
Php was a beginner, so it was severely frustrated at the beginning in SQL (specifically mysql. So write down some methods here. The first is the basic method. The basic method is not used now, so I will attach the database connection operation of some basic methods in hustoj: 1 if ($ OJ_SAE) {// for sae.sina.com.cn 2 mysql_connect (SAE _
Php was a beginner, so it was severely frustrated at the beginning in SQL (specifically mysql. So write down some methods here.
The first is the basic method. Now I don't need the basic method, so I will attach a part of hustoj.
Database Connection operations for the basic method:
1 if($OJ_SAE) {// for sae.sina.com.cn 2 mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS); 3 $DB_NAME=SAE_MYSQL_DB; 4 }else{ 5 //for normal install 6 if(!mysql_pconnect($DB_HOST,$DB_USER,$DB_PASS)) 7 die('Could not connect: ' . mysql_error()); 8 } 9 // use db10 mysql_query("set names utf8");11 12 if(!mysql_select_db($DB_NAME))13 die('Can\'t use foo : ' . mysql_error());
By the way, I used php to get started with the hustoj code and thanked hustoj for its open-source project. But I still want to talk about it. The front-end code of hustoj is too bad ..
This is the initialization operation before using the PDO method to operate SQL. (Because I want to run on SAE, I wrote two)
1 //db init2 if ($ON_SAE) {3 $pdo = new PDO('mysql:host='.SAE_MYSQL_HOST_M.';port='.SAE_MYSQL_PORT.';dbname='.SAE_MYSQL_DB, SAE_MYSQL_USER, SAE_MYSQL_PASS);4 $pdo->query("set names utf8;");5 } else {6 $pdo = new PDO("mysql:host=localhost;dbname=test","root","root");7 $pdo->query("set names utf8;");8 }
The advantage of PDO is that it prevents injection. However, some special methods are also needed. The following is an example of database operations.
General method (still part of the code from hustoj ):
1 $sql="SELECT * FROM `mail` WHERE `mail_id`=".$vid." and to_user='".$_SESSION['user_id']."'";2 $result=mysql_query($sql);3 $row=mysql_fetch_object($result);
The results are processed in a variety of ways and can be implemented as needed.
For the same part, the PDO write rule is:
1 $sql=$pdo->prepare("SELECT * FROM `tb_mail` WHERE `mail_id`=".$vid." and to_user='".$_SESSION['UID']."'");2 $sql->execute();3 $result=$sql->fetchAll();//$result[0]['content']
The results obtained by PDO are also processed in a variety of ways, as needed.
The advantage of PDO is that, in some cases, it is easier to prevent injection and so on to make database access more secure. The method is similar to the following:
It is a common practice.
This should be changed.
This is generally the case for accessing and operating php databases. The above is for your reference only. If there is a new technology, I will add it. If there is an error, please comment and correct it.