Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to merge a static data with json encode array data for output. Here is my php code:

$arr = array();

$rs = mysql_query("SELECT id, name, picture, mail, gender, birthday FROM users WHERE id='$logged_id' ");

while($obj = mysql_fetch_object($rs)) {
   $arr[] = $obj;
}

echo '{"users":'.json_encode($arr).'}';

Now I want to merge other data with it:

$user_ip = array("user_ip" => $user_ip_address);

I have tried array_merge($arr, $user_ip). But it didn't work. I think this is not correct json array format if I merge with existing data array. Please let me know what to do how to output other data as well as current data coming from mysql with json encode.

I am getting such output with my existing code, which is correct:

{"users":[{"id":"14","name":"Sonu Roy","picture":"image012.jpg","mail":"[email protected]","gender":"Male","birthday":"1983-01-11"}]}

But now I want to add other variable e.g $user_ip_address as user's data joining with current output data like this:

{"users":[{"id":"14","name":"Sonu Roy","picture":"image012.jpg","mail":"[email protected]","gender":"Male","birthday":"1983-01-11",user_ip:"127.0.0.1"}]}.

I want to get it in this way. How to do it? Please let me know. Thanks in advance.

share|improve this question
6  
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. – Neal Jan 11 at 15:33

2 Answers

try this:

echo json_encode(array('users' => $arr, 'user_ip' => $user_ip_address));

on a side note: you should use PHP PDO class to connect and query the database.

share|improve this answer

mysql_fetch_object returns an object, not an array. So, what are you doing by $arr[] = $obj; is just adding an object into an array. So, the actual structure of the $arr is something like

$arr => [
    [0] => Object(....),
    [1] => Object(....),
    ....
]

In your particular case, I assume you are fetching single row by primary key, so there are only one object.

THe simpliest way to fix this is to add a field to an object. I haven't worked with PHP since 5.3, so can't be sure, but it's as simple as adding

$obj->user_ip = $user_ip_address;

inside the loop.

Btw, a couple of questions for you:

  1. Why do you use loop if it should result in a single row?
  2. Why you are embedding the variable directly into SQL query. It's quite vulnerable, read about sql-injections and how to prevent it (one day I was really tired telling people here on SO to use PDO and prepared statements, so just go read about it),
  3. Have you read the documentation about mysql_fetch_object and array_merge? Why not (because if you have read it you wouldn't be asking the question).
  4. Tried debugging? E.g. attaching a debugger and watching for variables contents? Inserting logging or (God forgive me) debug print with echo?
  5. "Didn't work" is a quite bad error description. This time you was lucky, because the error was obvious. Next time, please, be more specific.
share|improve this answer
{"users":[{"id":"14","name":"Sonu Roy","picture":"image012.jpg","mail":"[email protected]","gender":"Male","birthd‌​ay":"1983-01-11"}]}. I am getting such output with my existing code, which is correct. But now I want to add other variable e.g $user_ip_address as user's data joining with current output data like this: {"users":[{"id":"14","name":"Sonu Roy","picture":"image012.jpg","mail":"[email protected]","gender":"Male","birthd‌​ay":"1983-01-11",user_ip:"127.0.0.1"}]}. – Neeraj Kumar Jan 11 at 16:34
Please, read the answer carefully. It is an answer. – J0HN Jan 11 at 18:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.