Reputation: 11
I have mysql database 5.2.6 charset utf8/utf8_general_Ci and i want to insert arabic words. They aren't inserted correctly with the words hashed php code.
<?php
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
mysql_query("set character_set_server='utf8'");
mysql_query("set names 'utf8'");
$con = mysqli_connect("localhost","root","root","mun_app") or die ("could not connect database");
$input = file_get_contents('php://input');
$data = json_decode($input, true);
$message = array();
if($data['action'] == "insert"){
$subject = $data['subject'];
$body = $data['body'];
$q = mysqli_query($con, "INSERT INTO `Complaints` ( `subject` , `body`, `Date`) VALUES ('$subject', '$body',CURDATE())");
}
echo mysqli_error($con);
?>
Upvotes: 0
Views: 304
Reputation: 142208
Do not mix mysql_*
and mysqli_*
API calls.
Do not use the mysql_*
API at all!
"not correctly" -- Do you have some sample output? See this for likely garbage and their causes.
Upvotes: 0
Reputation: 8162
First of all use a UTF8 encoding/collation on the tables and columns you want to add UTF8 data to.
Second Use statements for INSERT in DB:
$input = file_get_contents('php://input');
$data = json_decode($input, true);
$message = array();
if($data['action'] == "insert"){
$subject = $data['subject'];
$body = $data['body'];
$q = $con->prepare("INSERT INTO `Complaints` ( `subject` , `body`, `Date`) VALUES (?, ?,?)");
$date=CURDATE();
$q->bind_param("sss",$subject,$body,$date);
$q->execute();
}
Upvotes: 0
Reputation: 67
Two queries:
mysql_query("set character_set_server='utf8'");
mysql_query("set names 'utf8'");
got executed before connection is established. You can put them after mysqli_connect
and change to use created connection. Like:
mysqli_query($con, "set character_set_server='utf8'");
mysqli_query($con, "set names 'utf8'");
Also, be sure to check that incoming values of $subject and $body are correct.
Upvotes: 1