1. pdo introduction pdo (phpdataobject) is a new feature added to php5, because the php4php3 before php5 was a bunch of database tutorial extensions to deal with various data...
1. about pdo
Pdo (php 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 is used to operate mysql database instances,The code is as follows:
Exec ("insert into db_demo (name, content) values ('title', 'Content')") {echo "insert successful! "; Echo $ pdo-> lastinsertid () ;}?>
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 to use the first method. The result is that the name field only displays one 'C' character, and the subsequent places that should have displayed Chinese characters are blank. I only need to solve this problem: replace utf8 with gbk:
Pdo: :__ construct ($ dsn, $ user, $ pass, array (pdo: mysql_attr_init_command => "set names 'gbk ';"));
Second: Pdo ::__ construct ($ dsn, $ user, $ pass );
Pdo: exec ("set names 'utf8 ';");
In the second case, I also tested it in my environment. in this case, replace utf8 with gbk to display it. In addition, the pdo here :: replace $ pdo-> with $ pdo->. of course, this is a variable. you can define the variable name by yourself.
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 all these methods. In addition, I will introduce a method to solve Chinese garbled text here, but it is similar, basically no different from the third one, however, this method uses exec instead of query. the code is as follows:
$ Pdo-> exec ("set character set gbk ");
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 int $ debug enables debugging, if this function is enabled, the SQL statement int $ execrow is returned. whether to enable int $ lastinsertid returns the last insert record id string $ table database table string $ fields needs to insert the database field string $ values needs to insert database information, must correspond one to one with $ 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)") ;}/ * parameter specifies whether int $ debug enables debugging, when the SQL statement is enabled, the output SQL statement int $ execrow indicates whether execution is enabled and returns the number of entries. string $ table database table string $ set indicates the fields and content to be updated. format: a = 'abc ', B = 2, c = '1970-10-10 10:10:10 'string $ sqlwhere condition modification, allow 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 int $ debug indicates whether debugging is enabled, if the SQL statement is enabled, the int $ execrow statement is output. whether to enable the return number of execution items. string $ table database table string $ sqlwhere deletion condition. The value can be blank */function hrdelete ($ debug, $ execrow, $ table, $ sqlwhere = "") {global $ pdo; if ($ debug) {echo "delete from $ table where 1 = 1 $ sqlwhere"; exit; // open source code phprm.com} elseif ($ execrow) {return $ pdo-> exec ("delete from $ table where 1 = 1 $ sqlwhere ");} else {$ pdo-> query ("delete from $ table where 1 = 1 $ sqlwhere ");}}
Article address:
Reprint at will ^ please include the address of this article!