Home > Article > Backend Development > pdo connection data class and Chinese garbled solution_PHP tutorial
1.pdo introduction
pdo (php tutorial data object) is something added in php 5. It is a major new feature added in php 5, because before php 5, php4/php3 were a bunch of database tutorial extensions to connect and process each database. , what php_mysql tutorial.dll, php_pgsql.dll, php_mssql.dll, php_sqlite.dll and so on.
In php6, pdo will also be used by default to connect, and the mysql extension will be used as an auxiliary
2.pdo configuration
In php.ini, remove the ";" number in front of "extension=php_pdo.dll". If you want to connect to the database, you also need to remove the ";" number in front of the database extension related to pdo, and then restart the apache server.
extension=php_pdo.dll
extension=php_pdo_mysql.dll
extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
extension=php_pdo_mssql.dll
extension=php_pdo_odbc.dll
extension=php_pdo_firebird.dll
......
3.pdo connects to mysql database
new pdo("mysql:host=localhost;dbname=db_demo","root","");
The default is not a long connection. If you want to use a long connection to the database, you need to add the following parameters at the end:
new pdo("mysql:host=localhost;dbname=db_demo","root","","array(pdo::attr_persistent => true) ");
4. Common pdo methods and their applications
pdo::query() is mainly used for operations that return recorded results, especially select operations
pdo::exec() is mainly used for operations that do not return a result set, such as insert, update and other operations
pdo::lastinsertid() returns the last insertion operation, the primary key column type is the last auto-increment id
pdostatement::fetch() is used to get a record
pdostatement::fetchall() is to get all recordsets into one
5. pdo operates mysql database instance
Copy the code. The code is as follows:
$pdo = new pdo("mysql:host=localhost;dbname=db_demo","root","");
if($pdo -> exec("insert into db_demo(name,content) values('title','content')")){
echo "Insertion successful!";
echo $pdo -> lastinsertid();
}
?>
Copy the code The code is as follows:
$pdo = new pdo("mysql:host=localhost;dbname=db_demo","root","");
$rs = $pdo -> query("select * from test");
while($row = $rs -> fetch()){
print_r($row);
}
?>
The most commonly seen code on the Internet to solve Chinese garbled display is:
The first one: pdo::__construct($dsn, $user, $pass, array
(pdo::mysql_attr_init_command => "set names'utf8';"));
I tried the first method, but the result is that the name field only displays a 'c' character. After that, the place where Chinese should be displayed is blank.
The result is as follows: As shown in Figure 1
I just need to solve it: just replace utf8 with gbk, that’s it, that is:
pdo::__construct($dsn, $user, $pass, array(pdo::mysql_attr_init_command => "set
names'gbk';"));
Rendering 2 is as follows:
The second type: pdo::__construct($dsn, $user, $pass);
pdo::exec("set names 'utf8';");
I have also tested the second method in my environment. The display effect is shown in Figure 1. In this case, replace utf8 with gbk and it will be displayed
Showed. In addition, pdo:: here is replaced by $pdo-> when using it. Of course, this is a variable, and the variable name can be defined by yourself.
Third type
:$pdo->query('set names utf8;');
As for the third type, after reading the above two, you should also know that you need to replace utf8 with gbk, and it can be displayed correctly.
I have tested all of these. Either way. Ha ha. In addition, I also introduce here a method to solve Chinese garbled characters, but it is similar,
Basically, there is no difference between the third method and the third method. The difference is that this method does not use query but exec. The code is as follows:
$pdo->exec("set character set gbk");
/*
Common database operations, such as: add, delete, modify, query, obtain single record, multiple records, return the latest inserted record id, return the number of operation record rows, etc.
*/
/*
Parameter description
int $debug Whether to enable debugging, if enabled, the sql statement will be output
int $getcount whether to count, the return value is the number of rows
int $getrow whether to return a single record
string $table database table
string $fields Database fields to be queried, allowed to be empty, default is to search all
string $sqlwhere query condition, empty is allowed
string $orderby sorting, allowed to be empty, defaults to id reverse order
*/
function hrselect($debug, $getcount, $getrow, $table, $fields="*", $sqlwhere="", $orderby="id desc"){
global $pdo;
if($debug){
if($getcount){
echo "select count(*) from $table where 1=1 $sqlwhere order by $orderby";
}else{
echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
}
exit;
}else{
if($getcount){
$rs = $pdo->query("select count(*) from $table where 1=1 $sqlwhere order by $orderby");
return $rs->fetchcolumn();
}elseif($getrow){
$rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
return $rs->fetch();
}else{
$rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
return $rs->fetchall();
}
}
}
/*
参数说明
int $debug 是否开启调试,开启则输出sql语句
int $execrow 是否开启返回执行条目数
int $lastinsertid 是否开启返回最后一条插入记录id
string $table 数据库表
string $fields 需要插入数据库的字段
string $values 需要插入数据库的信息,必须与$fields一一对应
*/
function hrinsert($debug, $execrow, $lastinsertid, $table, $fields, $values){
global $pdo;
if($debug){
echo "insert into $table ($fields) values ($values)";
exit;
}elseif($execrow){
return $pdo->exec("insert into $table ($fields) values ($values)");
}elseif($lastinsertid){
return $pdo->lastinsertid("insert into $table ($fields) values ($values)");
}else{
$pdo->query("insert into $table ($fields) values ($values)");
}
}
/*
参数说明
int $debug 是否开启调试,开启则输出sql语句
int $execrow 是否开启执行并返回条目数
string $table 数据库表
string $set 需要更新的字段及内容,格式:a='abc',b=2,c='2010-10-10 10:10:10'
string $sqlwhere 修改条件,允许为空
*/
function hrupdate($debug, $execrow, $table, $set, $sqlwhere=""){
global $pdo;
if($debug){
echo "update $table set $set where 1=1 $sqlwhere";
exit;
}elseif($execrow){
return $pdo->exec("update $table set $set where 1=1 $sqlwhere");
}else{
$pdo->query("update $table set $set where 1=1 $sqlwhere");
}
}
/*
参数说明
int $debug 是否开启调试,开启则输出sql语句
int $execrow 是否开启返回执行条目数
string $table 数据库表
string $sqlwhere 删除条件,允许为空
*/
function hrdelete($debug, $execrow, $table, $sqlwhere=""){
global $pdo;
if($debug){
echo "delete from $table where 1=1 $sqlwhere";
exit;
}elseif($execrow){
return $pdo->exec("delete from $table where 1=1 $sqlwhere");
}else{
$pdo->query("delete from $table where 1=1 $sqlwhere");
}
}
?>