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

时间:2022-10-24 19:03:41
<?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);
?>