PHP PDO connect and query SQL database Code _php tutorial

Source: Internet
Author: User
Tags dsn mysql tutorial
PHP tutorial PDO Connect and query the 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 an advanced point.

$dbms = ' mysql ';//database Type the Oracle uses the ODI, for the developer, using different databases, just change this, do not remember so many functions
$host = ' localhost ';//database hostname
$dbname = ' Test '; Database used
$user = ' root ';//database connection user name
$pass = ';//password
$dsn = "$dbms: host= $host;d bname= $dbname";
Class DB extends PDO {
Public function __construct () {
try {
Parent::__construct ("$globals [DSN]", $globa ls[' 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 Instructions

The Pod (php data Object) extension is added in PhP5, and PHP6 connects the database with PDO, and all non-PDO extensions will be removed from the extension in PHP6. The extension provides PHP built-in class PDO to access the database, different databases use the same method name, to solve the problem of non-uniform database connection.
I was configured to do development under Windows.
The goal of PDO

Provides a lightweight, clear, and convenient API
Unify the common features of different RDBMS libraries, but not exclude more advanced features.
Provides an optional, large degree of abstraction/compatibility through PHP scripting.
Features of PDO:

Performance. PDO has learned from the outset the successes and failures of existing database extensions. Because PDO's code is brand new, we have the opportunity to start designing performance again to take advantage of the latest features of PHP 5.
Ability. PDO is designed to provide the basis for common database functionality while providing easy access to the unique features of the RDBMS.
Simple. PDO is designed to make it easy to use your database. The API does not force you into your code, and it clearly shows the process of each function call.
The runtime is extensible. The PDO extension is modular, allowing you to load drivers at run time for your database backend without having to recompile or reinstall the entire PHP program. For example, the PDO_OCI extension implements the Oracle database API instead of the PDO extension. There are also drivers for MySQL, PostgreSQL, ODBC, and Firebird, and more drivers are still being developed.
*/

http://www.bkjia.com/PHPjc/630848.html www.bkjia.com true http://www.bkjia.com/PHPjc/630848.html techarticle PHP Tutorial PDO connects and queries the SQL database 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.