时间:2021-07-01 10:21:17 帮助过:2人阅读
try{
$dsn = ‘mysql:host=localhost;dbname=myblog‘;
$username = ‘root‘;
$passwd = ‘123456‘;
$pdo = new PDO($dsn, $username, $passwd);
var_dump($pdo);
}catch(PDOException $e){
echo $e->getMessage();
}
二、URI形式
[php] view plaincopyprint?
try{
$dsn = ‘uri:file://F:\wamp\www\myBlog\dsn.txt‘;
$username = ‘root‘;
$passwd = ‘123456‘;
$pdo = new PDO($dsn, $username, $passwd);
var_dump($pdo);
}catch(PDOException $e){
echo $e->getMessage();
}
//dsn.txt
mysql:host=localhost;dbname=myblog
三、配置文件方式
[php] view plaincopyprint?
try{
$dsn = ‘mypdo‘;
$username = ‘root‘;
$passwd = ‘123456‘;
$pdo = new PDO($dsn, $username, $passwd);
var_dump($pdo);
}catch(PDOException $e){
echo $e->getMessage();
}
//其中在php.ini文件的任意位置加上以下代码,并保存重启服务器
pdo.dsn.mypdo="mysql:host=localhost;dbname=myblog"
PDO的三种连接数据库的方式
标签: