0

我对波兰语字符有疑问。当它应该是“克拉科夫”时,我无法正确书写单词,例如“?ukasz”而不是“Łukasz”甚至“null”。我在 mysql_connect 或 iconv(on json_encode($output)) 之后尝试了“mysql_set_charset('utf-8'/'iso-8859-1')”,它仍然是一样的(除了现在有“Krak\u00f3”而不是“无效的”)。我会很感激任何帮助。

这是我的 Android 应用程序的 php 文件:

$id_client = $_REQUEST['id_klienta'];
$con=mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('courier_helper') or die(mysql_error());

$sql=mysql_query("SELECT * FROM `clients` WHERE id_klienta='$id_client'");
while($r=mysql_fetch_assoc($sql))
    $output[]=$r;

print(json_encode($output));

mysql_close($con);

?>
4

2 回答 2

2

您必须确保在任何地方都使用 UTF-8:

  • 脚本文件编码(UTF-8 而不是 ANSI) - 您可以在 Notepad++ 中设置编码
  • html 代码(元字符集)
  • 数据库表字符集(创建表或数据库时)
  • 数据库mysql_set_charset('utf8', $connection_obj);
  • 数据库SET NAMES utf8- 连接后运行该 SQL 命令

还有一件事 - 熟悉 PDO。这是我使用的 PDO 连接功能:

function DbConnect()
{
    $db_host = "localhost";
    $db_name = "database_name";
    $db_user = "your_username";
    $db_pass = "your_passwd";

    $link = new PDO("mysql:host=$db_host;dbname=$db_name; charset=UTF-8", $db_user, $db_pass);
    $link->exec("set names utf8;");
    return $link;       
}

您可以像这样使用该函数(这是 PDO 示例):

    $link = DbConnect();

    $query = $link->prepare("SELECT id FROM wp_users");

    $query->execute();

    $result = $query->fetchAll(PDO::FETCH_ASSOC);
于 2013-03-09T21:04:33.953 回答
1

You should have your database storing data as UTF8, which means converting your existing tables.

ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

You also need to make sure your connection to the database is UTF8. You can make sure of that by running a SET NAMES query right after your connect.

SET NAMES UTF8

As others mentioned, you should start using PDO.

于 2013-03-09T21:06:50.460 回答