我正在开发一款
Android应用.在我的应用程序中,AsyncTask向PHP发送一个字符串. PHP返回一组裁剪名称.
我的php文件是:
<?php
ini_set('default_charset', 'utf-8');
header('Content-Type: text/html; charset=UTF-8');
mysql_connect("localhost","root","");
mysql_select_db("farm_o_pedia");
mysql_set_charset('utf8');
$result1 = mysql_query("SET NAMES utf8");
$lang=$_POST['LanguageName'];
$query1="select lang_id from lang_selection where lang_name='$lang'";
$lang_id=mysql_query($query1) or die(mysql_error());
$query2="select crop_name from crop_master where lang_id=$lang_id";
$result2=mysql_query($query2) or die(mysql_error());
while($row=mysql_fetch_assoc($result2))
{
$output[]=$row;
}
print(json_encode($output));
mysql_close();
?>
我在Logcat中遇到这种错误:
02-16 22:08:04.216: I/HTTP ok(1251): org.apache.http.message.BasicHttpResponse@4052f0b8
02-16 22:08:04.216 : I/JsonObj(1251): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘id #4’ at line 1
我不知道我的语法有什么问题.
EDIT: Used PDO as suggested and its working fine:
This is my new script.
<?php
$db = new PDO('mysql:host=localhost;dbname=farm_o_pedia;charset=utf8', 'root', '');
$db->query("SET NAMES utf8");
$lang=$_POST['LanguageName'];
$query1="select crop_name from crop_master where lang_id=(select lang_id from lang_selection where lang_name='$lang')";
$result2=$db->query($query1);
while(($row=$result2->fetch(PDO::FETCH_ASSOC))!=false)
{
$output[]=$row;
}
print(json_encode($output));
$db=null;
?>
最佳答案 我不确定您的查询的语法,但我知道您最好使用一个查询而不是两个查询.像这样的东西:
select crop_name
from crop_master cm join lang_selection ls on cm.languageid = ls.languageid
where lang_name = '$lang'
如果你不熟悉加入表格,我听过这本书的好消息,在10分钟内自学SQL