Home >Backend Development >PHP Tutorial >php pdo connects and queries sql database code_PHP tutorial
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 one
$dbms='mysql'; //Database type oracle uses odi. For developers, if they use different databases, they only need to change this, and there is no need to remember so many functions
$host='localhost'; //Database host name
$dbname='test'; //Database used
$user='root'; //Database connection username
$pass=''; //Corresponding 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 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 instructions
The pod (php data object) extension was added in php5. In php6, pdo will be used by default to connect to the database. All non-pdo extensions will be removed from the extension in php6. This extension provides php built-in class pdo to access the database. Different databases use the same method name to solve the problem of inconsistent database connections.
I configured it for development under windows.
■Pdo’s goal
Provide a lightweight, clear and convenient API
Unify features common to various rdbms libraries without excluding more advanced features.
Provides an optional greater degree of abstraction/compatibility via php scripts.
Performance. pdo has learned from the beginning about the successes and failures of scaling existing databases. Because the code for pdo was brand new, we had the opportunity to redesign performance from the ground up to take advantage of the latest features of PHP 5.
Simple. pdo is designed to make working with databases easy for you. The API doesn't force its way into your code, and the process of each function call is clearly shown.
Extensible at runtime. The pdo extension is modular and enables you to load drivers for your database backend at runtime 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, with more in development.
*/
http://www.bkjia.com/PHPjc/630848.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/630848.html