PHP PDO 连接数据库

<?php
$pdo = new PDO('mysql:host=localhost;dbname=test','root','password');
// 注意:如果有任何连接错误,将抛出一个 PDOException 异常对象。
?>  
<?php
try {
    $pdo = new PDO('mysql:host=localhost;dbname=test', $user, $password);
    foreach($pdo->query('SELECT * from `user`') as $value) {
        print_r($value);
    }
    $pdo = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
编码没设置
try{  
    $pdo=new PDO("mysql:host=localhost;dbname=test;charset=utf8","root","");  
    $res = $pdo->query('select * from user');

    foreach ($res as $key => $value) {
        var_dump($value);
    }
}catch (PDOException $e){  
    die("fail to connect mysql".$e->getMessage());  
}  

猜你喜欢

转载自blog.csdn.net/jxl9910/article/details/80567641