PHP PDO connect and query SQL database code

Source: Internet
Author: User
Tags foreach dsn mysql tutorial oracle database

$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 higher level of

$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
Unifies the common characteristics of various RDBMS libraries, but does not exclude more advanced features.
Provides an optional degree of abstraction/compatibility through PHP scripts.
Characteristics of PDO:

Performance. From the outset, PDO learned from the successes and failures of existing database extensions. Because PDO's code is brand new, 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 your database easy to use. The API does not force you into your code, and it clearly indicates the procedure for each function call. The
runtime can be extended. The PDO extension is 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, the PDO_OCI extension implements Oracle database APIs instead of PDO extensions. There are also a number of drivers for MySQL, PostgreSQL, ODBC, and Firebird, and more drivers are still in the process of development.
*/

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.