0

I am creating the android application to communicate from android device to my localhost server. I want to pass two arrays from php file to android. But I don't know how to do that. Please help me for that. I will search it a lot for this but I am not satisfy with my query.

Thanks in advance.

here is my php script.

<?php

// PHP variable to store the host address
$db_host  = "localhost";
// PHP variable to store the username
$db_uid  = "root";
// PHP variable to store the password
$db_pass = "";
// PHP variable to store the Database name  
$db_name  = "chatting"; 
// PHP variable to store the result of the PHP function 'mysql_connect()' which establishes the PHP & MySQL connection  
$db_con = mysql_connect($db_host,$db_uid,$db_pass) or die('could not connect');
mysql_select_db($db_name);

@$timestamp=$_POST["timestamp"];
@$message=$_POST["message"];
@$from=$_POST["from"];
@$to=$_POST["to"];
$from_id=1;//$_POST["from_id"];
$to_id=2;//$_POST["to_id"];

$sql1="INSERT INTO messages(from_msg,to_msg,message,timestamp) values('$from_id','$to_id','$message','$timestamp')";
$result1=mysql_query($sql1);
$sql = "SELECT * FROM messages";
$result = mysql_query($sql);
while($row=mysql_fetch_assoc($result))
$output[]=$row["message"];

$sql="SELECT username FROM users WHERE id='".$from_id"'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);

$sql1="SELECT username from users where id='".$to_id"'";
$result1=mysql_query($sql1);
$row1=mysql_fetch_array($result1);
$chat[]=array(
'from_msg'=>$row['username'],
'to_msg'=>row1['username']);
print(json_encode($output,$chat));
mysql_close();   
?>

I want to parse this arrays in android.

2
  • Why dont you just merge them in a single array? Commented Dec 26, 2013 at 9:01
  • you can't pass two array variables in json_encode() function, merge them and do pass the single array into json_encode() function. Commented Dec 26, 2013 at 9:06

1 Answer 1

3

either merge the arrays like

$newArray=array_merge($output,$chat);

Or create a multi dimensional array like

$newArray=array($output,$chat);

and finally

json_encode($newArray);

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

2 Comments

But I have a one question again is that how to decode them in android application?
this might help you javacodegeeks.com/2013/10/…

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.