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

时间:2022-09-25 20:11:29

Actually I am new to android web services so please help me my problem I am sending json encoded data from mobile client and I am getting json data on server side so that client side code:

实际上我是android web services的新手,请帮我解决我的问题,我从移动客户端发送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;

and php side I am doing

还有php方面

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

I am getting response

我得到响应

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

so how can I extract json data in php and insert in mysql

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

2 个解决方案

#1


1  

Do somehting like this..

做这样的服用点什么. .

<?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...

?>

#2


0  

here is a sample code

这是一个示例代码

I assume you know how to parse json from android. now in your server code use this to get the data from url and insert them to mysql

我假设您知道如何从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')");
   .....
   ..

if you have any doughts or have need more help just comment

如果你有任何双重或需要更多的帮助,请评论

#1


1  

Do somehting like this..

做这样的服用点什么. .

<?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...

?>

#2


0  

here is a sample code

这是一个示例代码

I assume you know how to parse json from android. now in your server code use this to get the data from url and insert them to mysql

我假设您知道如何从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')");
   .....
   ..

if you have any doughts or have need more help just comment

如果你有任何双重或需要更多的帮助,请评论