Phppdo connection and query sqldatabase code _ PHP Tutorial

Source: Internet
Author: User
Connect phppdo to and query SQL database code. Php Tutorial pdo connection and query sqldatabase tutorial code $ loginroot; $ passwdmysql tutorial; try {$ dbnewpdo (mysql: hostlocalhost; dbnamemysql, $ login, $ passwd ); foreach ($ db-query (php Tutorial pdo connection and query SQL database tutorial code
$ Login = "root ";
$ Passwd = "mysql tutorial ";
Try {
$ Db = new pdo ('MySQL: host = localhost; dbname = mysql', $ login, $ passwd );
Foreach ($ db-> query ('select * from test') as $ row ){
Print_r ($ row );
}
$ Db = null;
} Catch (pdoexception $ e ){
Echo $ e-> getmessage ();
}

Look at a more advanced

$ Dbms = 'mysql'; // Database type: oracle uses odi. for developers, if they use different databases, they do not need to remember so many functions.
$ Host = 'localhost'; // database host name
$ Dbname = 'test'; // database used
$ User = 'root'; // database connection username
$ Pass = ''; // password
$ Dsn = "$ dbms: host = $ host; dbname = $ dbname ";
Class db extends pdo {
Public function _ construct (){
Try {
Parent: :__ construct ("$ globals [dsn]", $ globals ['user'], $ globals ['pass']);
} Catch (pdoexception $ e ){
Die ("error:". $ e->__ tostring ()."
");
}
}
Public final function query ($ SQL ){
Try {
Return parent: query ($ this-> setstring ($ SQL ));
} Catch (pdoexception $ e ){
Die ("error:". $ e->__ tostring ()."
");
}
}
Private final function setstring ($ SQL ){
Echo "I want to handle $ SQL ";
Return $ SQL ;}}
$ Db = new db ();
$ Db-> setattribute (pdo: attr_case, pdo: case_upper );
Foreach ($ db-> query ('select * from xxxx_menu ') as $ row ){
Print_r ($ row );
}
$ Db-> exec ('delete from 'xxxx _ menu 'where mid = 43 ');

/*
About pdo

The pod (php data object) extension is added to php5. by default, pdo is identified in php6 to connect to the database. all non-pdo extensions will be removed from the extension in php6. This extension provides php built-in pdo class for accessing the database. different databases use the same method name to solve the problem of inconsistent database connections.
I configured it for development in windows.
■ Pdo goals

Provides a lightweight, clear, and convenient api
The common features of different rdbms databases are unified, but more advanced features are not excluded.
Php scripts provide optional abstract/compatibility.
■ Features of pdo:

Performance. From the very beginning, pdo learned from the success and failure of existing database expansion. Because the pdo code is brand new, we have the opportunity to design performance again to take advantage of the latest features of php 5.
Capability. Pdo is designed to provide common database functions as the basis and provide convenient access to the unique functions of rdbms.
Simple. Pdo is designed to make it easy to use databases. The api does not forcibly intervene in your code and clearly shows the process of calling each function.
It can be expanded during runtime. The pdo extension is modular, allowing you to load drivers at the backend of your database at runtime without re-compiling or re-installing the entire php program. For example, the pdo_oci extension replaces the pdo extension to implement the oracle database api. There are also some drivers for mysql, postgresql, odbc, and firebird. more drivers are still under development.
*/

Pdo connection and query sqldatabase tutorial code $ login = root; $ passwd = mysql tutorial; try {$ db = new pdo ('MySQL: host = localhost; dbname = mysql ', $ login, $ passwd); foreach ($ db-query ('...

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.