作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
它有Android应用程序将数据添加到数据库,但是当你用阿拉伯语输入数据时,展示 ”?????” 。我尝试了所有的方法。
文件config.inc.php:
<?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 项目中:Java 文件:
public class addnews extends Dialog {
private Activity mActivity;
private EditText mNewsTitleET;
private EditText mNewsET;
private Button mPublishBtn;
public String username;
JSONParser jsonParser = new JSONParser();
private String ADD_URL =
"http://yazanyazan3.esy.es/newssite/addnews.php";
public addnews(Activity mActivity)
{
super(mActivity);
this.mActivity = mActivity;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_addnews);
mNewsTitleET = (EditText) findViewById(R.id.title_news);
mNewsET = (EditText) findViewById(R.id.news_box);
mPublishBtn = (Button) findViewById(R.id.publish_btn);
Bundle use =getIntent().getExtras();
username = use.getString("name");
mPublishBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
attempAdding();
}
});
}
private void attempAdding()
{
if (!mNewsTitleET.getText().toString().equals("") &&
!mNewsET.getText().toString().equals(""))
{
new AddNewsTask().execute();
}
else
{
Toast.makeText(mActivity.getApplicationContext(),
"All fields are requires", Toast.LENGTH_LONG).show();
}
}
public Intent getIntent() {
return mActivity.getIntent();
}
private class AddNewsTask extends AsyncTask<Void, Void, Boolean>
{
private ProgressDialog mProgressDialog;
private JSONObject jsonObjectResult = null;
private String error;
@Override
protected void onPreExecute()
{
super.onPreExecute();
mProgressDialog = ProgressDialog.show(addnews.this.getContext(),
"Processing...", "Adding new news", false, false);
}
@Override
protected Boolean doInBackground(Void... params)
{
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("news_user",username));
pairs.add(new BasicNameValuePair("news_date", dateFormat.format(date).toString()));
pairs.add(new BasicNameValuePair("news_title", mNewsTitleET.getText().toString()));
pairs.add(new BasicNameValuePair("news_body", mNewsET.getText().toString()));
jsonObjectResult = jsonParser.makehttprequest(ADD_URL+"?user="+username+"", pairs);
if (jsonObjectResult == null)
{
error = "Error in the connection";
return false;
}
try
{
if (jsonObjectResult.getInt("success") == 1)
{
error = jsonObjectResult.getString("message");
return true;
}
else
error = jsonObjectResult.getString("message");
}
catch (Exception ex)
{
}
return false;
}
@Override
protected void onPostExecute(Boolean aBoolean)
{
super.onPostExecute(aBoolean);
mProgressDialog.dismiss();
Toast.makeText(mActivity.getApplicationContext(), error, Toast.LENGTH_LONG).show();
dismiss();
}
} }
在数据库中,我选择 utf8 。并尝试了所有的解决方案均无效。请帮忙。
最佳答案
希望这些提示能有所帮助:
尝试在附加文件中将静态文本添加到数据库中(不要从请求中读取它),这样您就可以将问题分为两部分,然后您可以查看问题是从Android客户端到PHP服务器还是从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
);
?>
现在检查“arabic_file”内容和数据库。
-如果文件不正常(包含乱码),那么问题出在您的 php 服务器上(比如它可能不支持阿拉伯语,...)。
-如果数据库不正常(再次乱码),那么问题可能出在您的数据库或 PHP 的 PDO 将数据插入数据库的方式,您应该指定连接的编码,例如:
$connection_str = "mysql:host=$host;dbname=$db;charset=utf8";
-如果一切正常,那么问题出在阿拉伯字符串来自 Android 设备的方式。
关于android - 如何从android将阿拉伯数据插入mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32917118/
我在 mysql 数据库中有一个“价格”字段,其中包含以阿拉伯或波斯数字表示的产品价格。 数字示例:12345567890 //1234567890 我不知道如何格式化它以便以用户友好的方式格式化。我
由于单独使用 prawn gem 时阿拉伯字母显示为未知字符,因此我安装了 Arabic-Prawn gem 0.0.1 以阿拉伯语打印数据。我用下面的代码作为测试 Prawn::Document.g
我是一名优秀的程序员,十分优秀!