如何提取json数据并插入mysql php

时间:2013-12-13 06:39:29

标签: php android mysql json

其实我是Android网络服务的新手所以请帮助我 我的问题我从移动客户端发送json编码数据,我在服务器端获取json数据,以便 客户端代码:

  mJobject.put("userName", contactname.getText().toString());
                     mJobject.put("phonenumber",phonenumber.getText().toString() );
                     mJArray.put(mJobject);
                     Log.v(Tag, "^============send request" + mJArray.toString());
                    contactparams.add(new BasicNameValuePair("contactdetails", mJArray.toString()));
                     Log.v(Tag, "^============send request params" + mJArray.toString());
                    jsonString=WebAPIRequest.postJsonData("http://localhost/contactupload/contactindex.php",contactparams);
public static String postJsonData(String url, List<NameValuePair> params) {
    String response_string = new String();

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
 //   httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
    try {
            httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));


          /*  String paramString = URLEncodedUtils.format(params, HTTP.UTF_8);
            String sampleurl = url + "" + paramString;
            Log.e("Request_Url", "" + sampleurl);*/

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            if (response != null) {
                    InputStream in = response.getEntity().getContent();
                    response_string = WebAPIRequest.convertStreamToString(in);

            }
    } catch (Exception e) {
            e.printStackTrace();
    }

    return response_string;

和php方面我正在做

<?php
$json_data=$_POST['contactdetails'];
$data=json_decode($json_data);
print_r($data);
?>

我收到回复

 Array
 (
   [0] => stdClass Object
    (
           [phone number] => 5555
           [username] => xfg
      )
 )

那么如何在php中提取json数据并在mysql中插入

2 个答案:

答案 0 :(得分:1)

做这样的事情..

<?php
$json_data=$_POST['contactdetails'];
$data=json_decode($json_data, true); // Added true flag

// You can access your variables like this..
echo $data['phone number'];// prints "5555"
echo $data['username']; // prints "xfg"

//do your db connection...

// execute your query with those variables...

?>

答案 1 :(得分:0)

这是一个示例代码

我假设您知道如何从android解析json。 现在在您的服务器代码中使用它来从url获取数据并将它们插入到mysql

// check for required fields
 if (isset($_POST['location']) && isset($_POST['email']) && isset($_POST['lat']) &&     isset($_POST['longitude'])) {

$location = $_POST['location'];
$email = $_POST['email'];
$lat = $_POST['lat'];
$longitude = $_POST['longitude'];

require_once 'config.php';
// connecting to mysql
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
// selecting database
mysql_select_db(DB_DATABASE);

 // mysql inserting a new row
 $result = mysql_query("INSERT INTO marked_locations(location, email,lat,longitude) VALUES('$location', '$email', '$lat','$longitude')");
   .....
   ..

如果您有任何想法或需要更多帮助,请发表评论