如果我想搜索文字而不是数字,我该如何更改我的代码?这是我的解析数据,当我输入数字时它运行但是当它的文本,我得到错误。谢谢你提前:)
//parse json data
try{
JSONObject object = new JSONObject(result);
String ch = object.getString("re");
if (ch.equals("success")) {
JSONObject no = object.getJSONObject("0");
// long q=object.getLong("f1");
String dn = no.getString("deceased_name");
String c = no.getString("company");
String ca = no.getString("company_address");
editText1.setText(c);
editText2.setText(ca);
editText3.setText(dn);
Toast.makeText(getApplicationContext(),
"Born: " + String.valueOf(c), Toast.LENGTH_LONG)
.show();
Toast.makeText(getApplicationContext(),
"Died: " + String.valueOf(ca), Toast.LENGTH_LONG)
.show();
Toast.makeText(getApplicationContext(),
"Died: " + String.valueOf(dn), Toast.LENGTH_LONG)
.show();
}
// php code
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("demo", $con);
$first_name=$_REQUEST['first_name'];
//$v1='111';
if($first_name==NULL)
{
$r["re"]="Enter the number!!!";
print(json_encode($r));
die('Could not connect: ' . mysql_error());
}
else
{
// $v1="530";
$i=mysql_query("select first_name from tblcontact where first_name=$first_name",$con);
$check='';
while($row = mysql_fetch_array($i))
{
$r[]=$row;
$check=$row['first_name'];
// print(json_encode($r));
}
if($check==NULL)
{
$r["re"]="Record is not available";
print(json_encode($r));
}
else
{
$r["re"]="success";
print(json_encode($r));
// die('Could not connect: ' . mysql_error());
}
}
mysql_close($con);
?>
答案 0 :(得分:0)
"select first_name from tblcontact where first_name = $first_name"
将此查询更改为
"select first_name from tblcontact where first_name = '". mysql_real_escape_string($first_name)."'"