Pdo connection data Class and Chinese garbled solution _ PHP Tutorial

Source: Internet
Author: User
Pdo connection data Class and Chinese garbled solution. 1. pdo introduction pdo (php Tutorial dataobject) is a new feature added to php5, because the php4php3 before php5 was a bunch of database tutorial extensions to follow 1. about pdo
Pdo (php Tutorial data object) is a new feature added to php 5, because php4/php3 before php 5 was a bunch of database tutorial extensions to connect to and process various databases, what php_mysql tutorial. dll, php_pgsql.dll, php_mssql.dll, php_sqlite.dll, and so on.
Php6 will also use the pdo connection by default, and mysql extension will be used as an auxiliary
2. pdo configuration
Php. in ini, remove the ";" sign before "extension = php_pdo.dll"; ". to connect to the database, remove the"; "sign before the pdo-related database extension, 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 connection to mysql database
New pdo ("mysql: host = localhost; dbname = db_demo", "root ","");
The default value is not persistent connection. to use persistent connection, 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 records, especially select operations.
Pdo: exec () is mainly used for operations that do not return result sets, such as insert and update operations.
Pdo: lastinsertid () returns the last insert operation. the primary key column type is the last auto-increment id of auto-increment.
Pdostatement: fetch () is used to obtain a record.
Pdostatement: fetchall () is to obtain all the record sets to
5. pdo for mysql database instances


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 "inserted successfully! ";
Echo $ pdo-> lastinsertid ();
}
?>


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 common code on the Internet to solve Chinese garbled text display is:

First: pdo ::__ construct ($ dsn, $ user, $ pass, array

(Pdo: mysql_attr_init_command => "set names 'utf8 ';"));

I tried the first method. The result is that the name field only displays one 'C' character. After that, the Chinese characters are displayed blank.

The result is as follows: 1 shows

I only need to solve this problem: replace utf8 with gbk directly, that is:

Pdo: :__ construct ($ dsn, $ user, $ pass, array (pdo: mysql_attr_init_command => "set

Names 'gbk ';"));

2 is as follows:


Type 2: pdo ::__ construct ($ dsn, $ user, $ pass );

Pdo: exec ("set names 'utf8 ';");

The second type is also tested in my environment. the display effect 1 shows. in this case, replace utf8 with gbk to display

. In addition, here pdo: $ pdo-> is used in place. of course, this is a variable, and the variable name can be customized.

Third

: $ Pdo-> query ('set names utf8 ;');

As for the third type, I have read the above two types. you should also know that you should replace utf8 with gbk, which can also be correctly displayed.

I have tested these methods. Yes. Haha. In addition, I will introduce a method to solve Chinese garbled text here, but it is similar,

There is nothing to do with the third method, but it doesn't work. This method uses exec instead of query. the code is as follows:

$ Pdo-> exec ("set character set gbk ");

/*

Common database operations, such as adding, deleting, modifying, and querying, obtaining a single record and multiple records, returning the latest insert record id and the number of Operation records
*/
/*
Parameter description
Whether debugging is enabled for int $ debug. if debugging is enabled, an SQL statement is output.
Whether int $ getcount is counted. the returned value is the number of rows.
Whether int $ getrow returns a single record
String $ table database table
String $ fields: the database field to be queried. it can be null. the default value is to search for all fields.
String $ sqlwhere query condition, which can be null
String $ orderby sorting, which can be null. the default value is id reverse.
*/

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 ();
}
}
}
/*
Parameter description
Whether debugging is enabled for int $ debug. if debugging is enabled, an SQL statement is output.
Whether int $ execrow enables the number of returned execution entries
Int $ lastinsertid whether to enable or not to return the last insert record id
String $ table database table
String $ fields to be inserted into the database
String $ values: The information of the database to be inserted. it must correspond to $ fields one by one.
*/
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 )");
}
}
/*
Parameter description
Whether debugging is enabled for int $ debug. if debugging is enabled, an SQL statement is output.
Int $ execrow whether to enable execution and return the number of entries
String $ table database table
String $ set field and content to be updated. format: a = 'abc', B = 2, c = '2017-10-10 10:10:10'
String $ sqlwhere condition for modification, which can be null
*/
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 ");
}
}
/*
Parameter description
Whether debugging is enabled for int $ debug. if debugging is enabled, an SQL statement is output.
Whether int $ execrow enables the number of returned execution entries
String $ table database table
String $ sqlwhere deletion condition, which can be null
*/
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 ");
}
}
?>

Pdo (php Tutorial data object) is a new feature added to php 5, because php4/php3 before php 5 are a bunch of database tutorial extensions to follow...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.