Php uses pdo to connect to and query the SQL database

Source: Internet
Author: User
Tags php database
This article describes how to use pdo to connect to and query SQL databases in php. Examples of commonly used pdo connection methods and improvement methods are analyzed, and the pdo technology is analyzed and explained, for more information, see

This article describes how to use pdo to connect to and query SQL databases in php. Examples of commonly used pdo connection methods and improvement methods are analyzed, and the pdo technology is analyzed and explained, for more information, see

This example describes how php connects to and queries the SQL database using pdo. Share it with you for your reference.

The specific implementation code is as follows:

The Code is as follows:

$ Login = "root ";
$ Passwd = "mysql ";
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 ();
}


Let's look at a more advanced one:

The Code is as follows:

$ 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:

A lightweight, clear, and convenient api is provided to unify the common features of different rdbms databases, but more advanced features are not excluded, php scripts provide optional abstract/compatibility.

Pdo features:

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, take advantage of the latest features of php 5.

Capability

Pdo is designed to provide common database functions as the basis, while providing convenient access to the unique functions of rdbms.

Simple

Pdo is designed to make it easy for you to use the database. APIs do not forcibly intervene in your code and clearly indicate the process of calling each function.

Runtime scalability

Pdo extensions are modular, allowing you to load drivers to the backend of your database at runtime without re-compiling or re-installing the entire php program, for example, pdo_oci extension will replace pdo extension to implement oracle Database APIs. There are also some drivers for mysql, postgresql, odbc and firebird. More drivers are still under development.

I hope this article will help you design php database programs.

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.