-3

I have this insertion PHP code :

<?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';
}
?> 

The retrieving PHP code from MySQL is:

<?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).'}';
?>

Problem:

The insertion and retrieving from MySQL gave ????????????? sign.

Note:

Inserted text in Arabic.

Question:

How to insert Arabic (Unicode) to MySQL?

Thanks in advance

halfer
  • 19,824
  • 17
  • 99
  • 186
ALMrsomi
  • 1
  • 3
  • Some of your downvotes (on a reasonably clear question otherwise) may be due to your use of all-caps (now edited out). This is often associated on the internet with shouting. – halfer Jul 10 '15 at 20:42

1 Answers1

0

You need to set the correct Collation (aka character encoding) on the table, as well as the Character Set on the table and/or Database as a whole to use which will have the needed arabic characters on the collation, otherwise unknown characters will appears as you have seen, as ? , I do not know which character encoding will be best for your chosen characters but take a look here for help saving Arabic characters to MySQL. Also check that you have the same encoding declared on your output HTML page else the page may try and render the characters with an incorrect default encoding, and giving you the same issue.

Store Arabic text in mysql database using php

Also as detailed by Chris85 in comments, UTF-8 all the way through is a very good link to read.

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132