0

I have written a PHP script below try to Post the JSON encoded string to Android device as below:

<?php
 // Create HostConnection
 $host = $_SERVER['HTTP_HOST'];  
 if ($host)
 {
    $data = array('HTTP_HOST' => $host);    
 }
 else
 {
    $data = array('HTTP_HOST' => "Not Available");
 }

 // Create Connection
 $link = mysql_connect('127.0.0.1:3306', 'root', 'xxxxxx');
 if ( $link != false )
 {
    //echo "mysql_connect success !";
    // Set Use UTF-8 String
    if (mysql_set_charset('utf8', $link)  && $db_selected = mysql_select_db("gw"))
    {
       // echo "mysql_set_charset() success<br />";       
       array_push($data, "SQL_DB", "gw");           
    } 
 }
 else
 {
   array_push($data, "SQL_DB", "Not Available");
 }       

 print_r(urldecode(json_encode($data)));
?>

Before post to Android device, I run this PHP script on the localhost(127.0.0.1) and got the result shown on the screen as:

{"HTTP_HOST":"127.0.0.1","0":"SQL_DB","1":"gw"}

Why there are 3 sets of data ? what I really want is 2 sets of data shown and prepare send to Android device on nest step. Where I was wrong ? If yes, what's the correct procedures to retrieve the encoded JSON array string on Android side ?

1 Answer 1

2

You are not pushing arrays to the array, but single elements. You can't add an associative array value using the method you show.

Use

 $data["SQL_DB"] = "gw";     

instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Great, Follows Pekka's instruction to modify the PHP script, It's work and meet my expection !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.