如何从android将阿拉伯语数据插入mysql数据库
它有Android应用程序向数据库添加数据, 但当你在阿拉伯世界输入数据时, 显示“???????”。 我尝试了所有的方法 文件config.inc.php:如何从android将阿拉伯语数据插入mysql数据库,android,mysql,database,internationalization,arabic,Android,Mysql,Database,Internationalization,Arabic,它有Android应用程序向数据库添加数据, 但当你在阿拉伯世界输入数据时, 显示“???????”。 我尝试了所有的方法 文件config.inc.php: <?php $username = "*****"; $password = "****"; $host = "*****"; $dbname = "*****"; $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); try
<?php
$username = "*****";
$password = "****";
$host = "*****";
$dbname = "*****";
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try
{
$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
mysql_set_charset('utf8');
}
catch(PDOException $ex)
{
die("Failed to connect to the database: " . $ex->getMessage());
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
function undo_magic_quotes_gpc(&$array)
{
foreach($array as &$value)
{
if(is_array($value))
{
undo_magic_quotes_gpc($value);
}
else
{
$value = stripslashes($value);
}
}
}
undo_magic_quotes_gpc($_POST);
undo_magic_quotes_gpc($_GET);
undo_magic_quotes_gpc($_COOKIE);
}
?>
添加文件:
<?php
require("config.inc.php");
$query = "INSERT INTO news ( news_date, news_title, news_body ) VALUES ( :news_date, :news_title, :news_body ) ";
$query_params = array(
':news_date' => $_POST['news_date'],
':news_title' => $_POST['news_title'],
':news_body' => $_POST['news_body']
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex)
{
$response["success"] = 0;
$response["message"] = "Database Error. Couldn't add News!";
die(json_encode($response));
}
$response["success"] = 1;
$response["message"] = "News Successfully Added!";
echo json_encode($response);
?>
希望这些提示能有所帮助:
尝试在添加文件中将静态文本添加到数据库中(不要从请求中读取),这样您可以将问题分为两部分,您可以查看问题是从android客户端到PHP服务器还是从PHP服务器到数据库,如:
<?php
/*
please type the Arabic string(salam) with your own keyboard and do not
copy my version because I used Persian keyboard.
*/
$arabic_string = 'سلام';
file_put_contents("arabic_file", $arabic_string);
$query_params = array(
':news_date' => $_POST['news_date'],
':news_title' => $_POST['news_title'],
':news_body' => $arabic_string
);
?>
-如果一切正常,那么问题在于阿拉伯语字符串来自android设备的方式。尝试在添加文件中向数据库添加静态文本(不要从请求中读取)所以你可以把问题分为两部分,你可以看到问题是从android客户端到php服务器还是从php服务器到数据库。请,你能在文件中编辑我的代码吗?我希望你能帮助我,我已经失去了希望。
<?php
/*
please type the Arabic string(salam) with your own keyboard and do not
copy my version because I used Persian keyboard.
*/
$arabic_string = 'سلام';
file_put_contents("arabic_file", $arabic_string);
$query_params = array(
':news_date' => $_POST['news_date'],
':news_title' => $_POST['news_title'],
':news_body' => $arabic_string
);
?>
$connection_str = "mysql:host=$host;dbname=$db;charset=utf8";