Blogger Information
Blog 55
fans 0
comment 0
visits 50507
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP-PDO的数据库连接操作-0830
Bean_sproul
Original
685 people have browsed it

PDO连接数据库的操作

设置数据库的链接类型

$dsn = 'mysql:host=127.0.0.1; dbname=test';

实例化PDO类,创建pdo对象,并完成了数据库的连接

$pdo = new PDO($dsn, $user, $pass);

实例

<?php 
// PDO php数据对象,是PHP操作所有数据库的抽象层,提供了一个统一访问的接口

//数据源:设置数据库的类型
$dsn = 'mysql:host=127.0.0.1; dbname=test';

$user ='root';

$pass ='root';
//实例化PDO类,创建pdo对象,并完成了数据库的连接
try {

    $pdo = new PDO($dsn, $user, $pass);
    echo '<h2>连接成功</h2>';

} catch (PDOException $e)//PDO的一场处理 
{
    die('Connect ERROR! :'. $e->getMessage());
}

// 关闭连接
//$pdo = null;

//unset($pdo);
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

 

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post