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); ?>
本文出自 无忧之路 - 博客园