php PDO连接mysql以及字符乱码处理

<?php
//mysql 的  PDO

$dsn = "mysql:dbname=cqkx;host:localhost";
$username = "root";
$password = "";
try{
    $pdo = new PDO($dsn, $username, $password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"));//最后一个参数使用utf8
    
}catch (PDOException $e) {
    echo "error:".$e->getMessage();
    exit;
}

$result = $pdo->prepare("select * from t_user");
$result->execute();
$return = $result->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($return);
?>

 

posted @ 2013-12-15 12:02  无忧之路  阅读(449)  评论(0编辑  收藏  举报
无忧之路