Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php android与MYSQL数据库连接返回空值_Php_Android_Json - Fatal编程技术网

Php android与MYSQL数据库连接返回空值

Php android与MYSQL数据库连接返回空值,php,android,json,Php,Android,Json,我试图从MySQL数据库中检索特定数据,方法是从Android中传递参数值,然后在查询中的PHP脚本中读取该值以返回数据 当我运行应用程序时,由于返回的结果值为null,因此在分析数据时发生错误异常 为什么结果为空?错误是来自PHP脚本还是来自我的java代码 请帮帮我 提前谢谢 city.php: <?php mysql_connect("localhost","username","password"); mysql_select_db("Countries")

我试图从MySQL数据库中检索特定数据,方法是从Android中传递参数值,然后在查询中的PHP脚本中读取该值以返回数据

当我运行应用程序时,由于返回的结果值为null,因此在分析数据时发生错误异常

为什么结果为空?错误是来自PHP脚本还是来自我的java代码

请帮帮我

提前谢谢

city.php:

  <?php
     mysql_connect("localhost","username","password");
     mysql_select_db("Countries");
     $sql=mysql_query("select  City_Population  from City where Name= "'.$_REQUEST['Name']."'");
     while($row=mysql_fetch_assoc($sql))
     $output[]=$row;
      print(json_encode($output));
      mysql_close();
        ?>
       public class ConnectActivity extends ListActivity {

           String add="http://10.0.2.2/city.php";
           public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);

            new Connect().execute();

         }

  private class Connect extends AsyncTask<Void,Void,String>
   {     
             private  String result = "";
             private  InputStream is=null;
            private  String city_name="London";
           protected String doInBackground(Void... params) {
            try
          {
                  ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                 nameValuePairs.add(new BasicNameValuePair("Name",city_name));
                 HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(add);
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
               HttpResponse response = httpclient.execute(httppost);
               HttpEntity entity = response.getEntity();
              is = entity.getContent();
                 }
        catch(Exception e)
           {
               Log.e("log_tag", "Error in http connection "+e.toString());
                 }


           //convert response to string
    try{
    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
    StringBuilder sb = new StringBuilder();
     String line = null;
     while ((line = reader.readLine()) != null) {
      sb.append(line + "\n");
       }
        is.close();
        result=sb.toString();
           }
          catch(Exception e){
           Log.e("log_tag", "Error converting result "+e.toString());
               }


          return result;
          }
       protected  void onPostExecute(String  result){

        try{
            JSONArray jArray = new JSONArray( result);
            JSONObject json_data=null;
            for(int i=0;i<jArray.length();i++)
            {
                json_data = jArray.getJSONObject(i);
                int  population=json_data.getInt("City_Population");

              TextView City_Name =(TextView)findViewById(R.id.city_name);
                                                                           TextView  City_population=(TextView)findViewById(R.id.city_pop);
                            City_Name.setText(json_data.getString(city_name));
                                                                          City_population.setText(population+"  " );
            }
            }
            catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
            }


                     }
                                                                  }

                                                    }
a)您的脚本容易出错。在将$_REQUEST[…]参数放入sql查询字符串之前,需要对其进行正确编码。
b) 您需要一些错误处理。任何mysql函数都可能失败,您的脚本必须处理这些错误情况。因为客户端需要一些json数据,所以也可以将错误消息/代码作为json编码的数组返回。
c) 您可能希望将
内容类型
标题设置为
application/json
,请参阅和


听着,你能告诉我在mysql中,City_人口正在检索或多个数据吗
     <?php
         $name=$_POST['NAME'];               
         mysql_connect("localhost","username","password");
         mysql_select_db("Countries");
         $sql=mysql_query("select  City_Population as citypop  from City where Name='$name' ");
         while($row=mysql_fetch_assoc($sql))
          $output=$row['citypop'];
        print(json_encode($output));
         mysql_close();
         ?>
<?php
define('DEBUG_DETAILS', true);
function onError($msg, $details) {
    $msg = array(
        'status'=>'error',
        'message'=>$msg
    );
    if ( defined('DEBUG_DETAILS') && DEBUG_DETAILS ) {
        $msg['details'] = $details;
    }
    die(json_encode($msg));
}


$mysql = mysql_connect("localhost","username","password") or OnError('database connection failed', mysql_error());
mysql_select_db("Countries", $mysql) or OnError('database selection failed', mysql_error($mysql));

$query = "
    SELECT
        City_Population
    FROM
        City
    WHERE
        Name='%s'
";
$query = sprintf($query, mysql_real_escape_string($_REQUEST['Name'], $mysql));
$sql=mysql_query($query, $mysql) or OnError('query failed', array('query'=>$query, 'errstr'=>mysql_error($mysql)));

$output = array(
    'count'=>0,
    'records'=>array()
);
while( $row=mysql_fetch_assoc($sql) ) {
    $output['records'][]=$row;
    $output['count']+=1;
}
echo json_encode(array(
    'status'=>'ok',
    'result'=>$output
));
{
  status:"ok",
  result: {
    'count': 2,
    'records': [ 10000, 15000]
  }
}
{
  status:"error",
  message: "database connection failed",
  setails: "...."
}