Use of PDO

Source: Internet
Author: User
Use of PDO, read the use of PDO, // first connect to the mysql database $ dbh = newPDO ("> <LINKhref =" http://www.php100.com//statics/style/headfloor_950_081205.css "type = text/cssrel = stylesheet> <LINKh

// Connect to the mysql database first
$ Dbh = new PDO ('MySQL: host = localhost; dbname = test', $ user, $ pass );
// If you want to connect to mssql:
// Mssql: host = localhost; dbname = testdb
// Connect to pgsql:
// Pgsql: host = localhost port = 5432 dbname = testdb user = bruce password = mypass
// Connect to odbc (DSN)
// Odbc: testdb
// Access:
// Odbc: Driver = {Microsoft Access Driver (*. mdb)}; Dbq = C: \ db. mdb; Uid = Admin
// Oracle, sqlite, db2 ....

// Execute a query
Foreach ($ dbh-> query ('select * from Foo') as $ row ){
Print_r ($ row); // The result is similar to that of mysql_fetch_array. PDOStatement: setFetchMode can be adjusted.
}

// You can also:
$ Something = $ dbh-> prepare ("SELECT name, color FROM fruit ");
$ Something-> execute ();

// Read the entire record set to the array:
$ Result = $ Something-> fetchAll ();
Print_r ($ result );
// Output:
Array
(
[0] => Array
(
[NAME] => pear
[0] => pear
[COLOUR] => green
[1] => green
)

[1] => Array
(
[NAME] => watermelon
[0] => watermelon
[COLOUR] => pink
[1] => pink
)

)

// Insert/delete/update data:
$ Count = $ dbh-> exec ("delete from fruit WHERE color = 'red '");
// $ Count indicates the number of deleted items. Equivalent to mysql_affected_rows
// PDOStatement: rowCount is also available

// I forgot what database I used ....
If ($ db-> getAttribute (PDO: ATTR_DRIVER_NAME) = 'mysql '){
Echo "Running on mysql; doing something mysql specific here \ n ";
}

// Use mysql_escape_string when inserting data?
Print "Unquoted string: $ string \ n ";
Print "Quoted string:". $ conn-> quote ($ string). "\ n ";
// Obtain:
Unquoted string: Nice
Quoted string: 'Nice'
// You can see that quotation marks are automatically added ....
// Note that the results in different databases are different, for example, some '=> '', some' => \ ', \=> \\
// No worries, fully automated.

// Even close it.
$ Conn = null;
//! You can keep the connection:
$ Dbh = new PDO ('odbc: sample', 'db2inst1', 'ibmdb ',
Array (PDO_ATTR_PERSISTENT => true ));

// Isn't it easy?

Appendix: Special simple call methods:
$ Stmt = $ dbh-> prepare ("SELECT * from registry where name =? ");
If ($ stmt-> execute (array ($ _ GET ['name']) {// What are you afraid? Automatic quote!
While ($ row = $ stmt-> fetch ()){
Print_r ($ row );
}
}

You can also:
$ Stmt-> bindParam (1, $ id );
$ Stmt-> bindParam (2, $ _ FILES ['file'] ['type']);
$ Stmt-> bindParam (3, $ fp, PDO: PARAM_LOB );

Where can I find such a good function? Php5.1 and above are in extension, php5 is in pecl, php4? Don't think about it. no.

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.