How PHP uses PDO to connect and query SQL databases _php tips

Source: Internet
Author: User
Tags dsn php database oracle database

This example describes how PHP uses PDO to connect and query SQL databases. Share to everyone for your reference.

The specific implementation code is as follows:

Copy Code code 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 ();
}

Look at a more advanced point:
Copy Code code as follows:
$dbms = ' MySQL '; Database type Oracle with ODI, for developers, using a different database, just change this, do not have to remember so many functions
$host = ' localhost '; Database host Name
$dbname = ' Test '; The database used
$user = ' root '; Database connection user Name
$pass = '; The corresponding password
$DSN = "$dbms: host= $host;d bname= $dbname";
Class DB extends PDO {
Public Function __construct () {
try {
Parent::__construct ("$globals [DSN]", $globals [' User '], $globals [' Pass ']);
catch (Pdoexception $e) {
Die ("Error:"). $e->__tostring (). "<br/>");
}
}
Public final function query ($sql) {
try {
Return Parent::query ($this->setstring ($sql));
}catch (Pdoexception $e) {
Die ("Error:"). $e->__tostring (). "<br/>");
}
}
Private Final function setstring ($sql) {
echo "I want to deal with $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 Description

The Pod (php data Object) extension is added to the PHP5, php6 the PDO connection to the database, and all PDO extensions will be removed from the extension in PHP6. This extension provides PHP built-in class PDO to access the database, and different databases use the same method name to solve the problem that the database connection is not unified.

I am configured to do development under Windows.

PDO's Goal:

Provides a lightweight, clear, and convenient API that unifies the common features of various RDBMS libraries, but does not exclude more advanced features that provide an optional degree of abstraction/compatibility through PHP scripts.

Characteristics of PDO:

Performance, PDO has learned from the outset the success and failure of existing database extensions, because the PDO code is brand new, so we have the opportunity to restart design performance to take advantage of the latest features of PHP 5.

Ability

PDO is designed to provide common database functionality as a foundation, while providing easy access to the unique features of the RDBMS.

Simple

PDO is designed to make it easy for you to use a database, the API does not force you into your code, and it clearly indicates the process of each function call.

Run-time extensible

PDO extensions are modular, enabling you to load drivers for your database backend at run time without having to recompile or reinstall the entire PHP program, for example, PDO_OCI extensions implement the Oracle database APIs instead of PDO extensions, and some for MySQL, Drivers for PostgreSQL, ODBC, and Firebird, and more drivers are still in the process of development.

I hope this article will help you with your PHP database program design.

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.