从MySQL插入和检索返回??????????????阿拉伯文字的标志

时间:2015-07-10 19:52:17

标签: php mysql json

我有这个插入PHP代码:

<?PHP 
require("config.inc.php");
if (!empty($_POST)) {   
    $query = "INSERT INTO projects ( project_author, project_title, project_spec, project_links, project_desc,created_at ) VALUES ( :user, :title, :spec, :links, :desc, NOW() ) ";
    $query_params = array(
        ':user' => $_POST['author'],
        ':title' => $_POST['title'],
        ':spec' => $_POST['spec'],
        ':links' => $_POST['links'],
        ':desc' => $_POST['desc'],
    );
    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        $response["success"] = 0;
        $response["message"] = "Database Error. Couldn't add post!";
        die(json_encode($ex));
    }

    $response["success"] = 1;
    $response["message"] = "Post Successfully Added!";
    echo json_encode($response);

}else{
echo 'omar almrsomi';
}
?> 

从MySQL检索PHP代码是:

<?php
$mysql_db_hostname = "com";
$mysql_db_user = "xxxxx";
$mysql_db_password = "xxxxxxxxxx";
$mysql_db_database = "xxxxxxxxx";

$con = @mysqli_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password,
 $mysql_db_database);

if (!$con) {
 trigger_error('Could not connect to MySQL: ' . mysqli_connect_error());
}
$var = array();
$sql = "SELECT * FROM projects";
$result = mysqli_query($con, $sql);

while($obj = mysqli_fetch_object($result)) {
$var[] = $obj;
}
echo '{"projects":'.json_encode($var).'}';
?>

问题:

从MySQL插入和检索给了?????????????登录。

注意:

用阿拉伯语插入文字。

问题:

如何将阿拉伯语(Unicode)插入MySQL?

提前致谢

1 个答案:

答案 0 :(得分:0)

您需要在表格上设置正确的排序规则(也就是字符编码),以及表格和/或数据库中的Character Set作为一个整体使用哪个将具有整理时需要的阿拉伯字符,否则会出现未知字符,如?,我不知道哪种字符编码最适合您选择的字符,但请查看此处以帮助保存阿拉伯字符MySQL的。还要检查您在输出HTML页面上是否声明了相同的编码,否则页面可能会尝试使用不正确的默认编码呈现字符,并为您提供相同的问题。

Store Arabic text in mysql database using php

另外,Chris85在评论中详细说明,UTF-8 all the way through是一个非常好的阅读链接。