Home >Backend Development >PHP Tutorial >Simple use of php pdo (1)

Simple use of php pdo (1)

WBOY
WBOYOriginal
2016-08-08 09:25:141051browse

Introduction:

PDO extension defines a lightweight, consistent interface for PHP to access databases. It provides a data access abstraction layer so that no matter what database is used, queries and retrieve data.

It provides a database access abstraction layer that allows us to operate different databases through consistent functions and writing methods, which is beneficial to future database migrations and of course also improves security.

Comparison:

Commonly used methods for php operating databases, taking mysql as an example, include php_mysql, php_mysqli, pdo

1.php_mysql and php_mysqli are not portable and can only be applied to mysql databases, while pdo can be easily transplanted .

2.php_mysql is a function that we learn to operate the database when we are new to PHP, but in fact it is rarely used. The most important point is that it can easily cause security problems.

There is SQL injection, so PHP provides the function mysql_real_escape_string. If there are many variables, each one must go through mysql_real_escape_string

Instead it becomes very troublesome.

3.pdo supports preprocessing. The preprocessing function can effectively avoid SQL injection and improve efficiency.

4.php_mysqli and pdo both support object-oriented.

5.pdo The long connection method has better performance than php_mysqli (refer to online information)

php_mysql method has little advantage. The most fatal thing about php_mysqli is that it cannot be transplanted to other databases unless you are sure that you will not change the database. . Obviously the pdo method of connecting to the database will be a trend.

pdo simple operation of the database:

First of all, php needs to enable php-pdo related extensions

<?php

$dbType = &#39;mysql&#39;;
$dbUser = &#39;root&#39;;
$dbPass = &#39;simael&#39;;
$dbhost = &#39;localhost&#39;;
$dbName = &#39;pdotest&#39;;
$dsn="$dbType:host=$dbhost;dbname=$dbName";
try{
    $pdo = new PDO($dsn, $dbUser, $dbPass); 
    echo "PDO成功连接MySQL数据库!";
}catch(PDOException $exception){
    echo $exception->getMessage();
}

Insert data

<?php
//$pdo->query('set names utf8');
$sql = "INSERT INTO la_comments SET nick_name='formPdo',email='pdo@pdo.com',comment='中文comment',page_id='11',created_at=NOW(),updated_at=NOW()";
$res1 = $pdo->exec($sql);
var_dump($res1);

At this time, when I go to the database, I see that the Chinese characters may be garbled, because of encoding problems. The reason

adding code

$pdo->query('set names utf8');
data storage still has errors is because it is possible that your header is gbk encoded

The coding issues summarized online that need to be paid attention to are as follows:

1. Server program declares encoding. For example, header("Content-type: text/html; charset=utf-8");
2. The client declares the encoding, such as
3. Database encoding, table and field encoding
4. Encoding properties of the file itself
5. Please declare the encoding when connecting to the database, such as $pdo->query('set names utf8');

As long as the above are consistent, the encoding problem will generally be solved.

Full sample code: (the file itself should also be encoded in utf8)

$dbType = 'mysql';
$dbUser = 'root';
$dbPass = 'simael';
$dbhost = 'localhost';
$dbName = 'pdotest';
$dsn="$dbType:host=$dbhost;dbname=$dbName";
try{
	$pdo = new PDO($dsn, $dbUser, $dbPass,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES'utf8';")); 
	//$pdo = new PDO($dsn, $dbUser, $dbPass); 
    echo "PDO成功连接MySQL数据库!";
}catch(PDOException $exception){
	echo $exception->getMessage();
}

$pdo->query('set names utf8');
$sql = "INSERT INTO la_comments SET nick_name='formPdo',email='pdo@pdo.com',comment='中文comment',page_id='11',created_at=NOW(),updated_at=NOW()";
$res1 = $pdo->exec($sql);
var_dump($res1);

$sql = "SELECT * FROM la_comments";
$res2 = $pdo->query($sql);
while($row = $res2->fetch()){
	print_r($row);
	echo '
'; } $pdo = null; ?>




The above introduces the simple use of php pdo (1), including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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