MYSQL / PHP斯堪的纳维亚字母 [英] MYSQL/PHP scandinavian letters

查看:73
本文介绍了MYSQL / PHP斯堪的纳维亚字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP文件,该文件从mysql表获取数据,然后json对其进行编码。一切正常,直到mysql中有斯堪的纳维亚字母的行为止。然后,它仅返回null

I have a PHP file that gets data from mysql table and then json encodes it. Everything works fine until there is a row in mysql that has scandinavian letters. Then it only returns null

  {"key":[{"Name":null}]}

我如何使其工作?是PHP还是MYSQL中的问题?我试图将 header('content-type:text / html; charset:utf-8'); 放在php文件的开头,但这没用

How can i make it work? Is the problem in PHP or in MYSQL? I tried to put header('content-type: text/html; charset: utf-8'); to the beginning of the php file, but that didn't work

<?php
header('content-type: text/html; charset: utf-8');
include 'config.php';


$sql = "Select * from table;



try {
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->query($sql);  
    $results = $stmt->fetchAll(PDO::FETCH_OBJ);
    $dbh = null;
    echo '{"key":'. json_encode($results) .'}'; 
} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}


?>

当我使用此代码时:

<?php
$con = mysql_connect("localhost","adsasd","asdasd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("table", $con);

$result = mysql_query("SELECT NAME from table where ID=1");

while($row = mysql_fetch_array($result))
  {
  echo $row['TABLE'];
  }

mysql_close($con);
?> 

它显示的字母应为:äöåäö

It shows the letter as they should be: äöåäö

问题出在 json_encode()部分吗?
为什么它使整个行显示为null而不是仅仅使斯堪的纳维亚字母显示为``符号或其他东西?''

So the problem is in the json_encode() part? And why does it make the whole row appear as null instead of just making the scandinavian letters to appear as � signs or something?

推荐答案

您应该使用 $ dbh-> query('SET CHARACTER SET utf8');

这篇关于MYSQL / PHP斯堪的纳维亚字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆