Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8901827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:27:28+00:00 2026-06-15T01:27:28+00:00

I have a php array like the one below. $arr = array( array( date=>date(‘Y-m-d’),

  • 0

I have a php array like the one below.

$arr = array(
                array(
                    "date"=>date('Y-m-d'),
                    "message"=>"test message 1",
                    "from_id"=>21,
                    "to_id"=>14
                ),
                array(
                    "date"=>date('Y-m-d'),
                    "message"=>"test message 2",
                    "from_id"=>23,
                    "to_id"=>12
                )               
            );

I do a json_encode on this array to convert it json.

$jsonarr =  json_encode($arr);

Output of $jsonarr:

[
 {"date":"2012-11-22","message":"test message 1","from_id":21,"to_id":14},
 {"date":"2012-11-22","message":"test message 2","from_id":23,"to_id":12}
]

After that i insert this in mysql table. Everything is fine till here.

Now i have to update this with another json by concatenation.

Another JSON:

[
 {"date":"2012-11-22","message":"test message 3","from_id":28,"to_id":2},
 {"date":"2012-11-22","message":"test message 4","from_id":53,"to_id":72}
]

And i want the field in my mysql table to display like this:

[
 {"date":"2012-11-22","message":"test message 1","from_id":21,"to_id":14},
 {"date":"2012-11-22","message":"test message 2","from_id":23,"to_id":12},
 {"date":"2012-11-22","message":"test message 3","from_id":28,"to_id":2},
 {"date":"2012-11-22","message":"test message 4","from_id":53,"to_id":72}
]

How can i write an UPDATE query for this.

I can do this by using 2 queries. One select query then formatting the field and later updating. But can this be achieved with one query ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:27:29+00:00Added an answer on June 15, 2026 at 1:27 am

    The solution to your immediate problem is this:

    MySQL does not natively support JSON values. You need to fetch the value from the database:

    SELECT FOR UPDATE my_json_field
    FROM my_table
    WHERE id = 555;
    

    Then modify it in the PHP script:

    $my_array = json_decode($value_of_my_json_field);
    $my_array = array_merge($my_array, $arr);
    $value_of_my_json_field = json_encode($my_array);
    

    Then update it in the DB:

    UPDATE my_table
    SET my_json_field = :value_of_my_json_field
    WHERE id = 555;
    

    Make sure this happens in a transaction. Also, you need to use the InnoDB storage engine.

    But the more interesting question is why do you need to store JSON in the DB? Why not use a separate table:

    • Table: messages
      • id integer
      • date timestamp
      • from_id integer (this should be a foreign key)
      • to_id integer (this should be a foreign key)

    Then you could simply insert into that table. That’s the correct way of doing this. If you don’t have a very good reason to use JSON, I would suggest getting rid of it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an array in php like this: $arr['test'] = asd; $arr['test1'] = asd1;
I have converted an array to object data like this: <?php $myobject->data = (object)Array('zero','one','two');
I have an Array like below in PHP array( (int) 0 => array( 'id'
I have a form like the one below which is posted to contacts.php ,
Say I have a simple PHP loop like this one // Bad example $array
I have a PHP array like below: Array ( [0] => 16 [1] =>
I have a .php file that contains array data like below: $config['setting_one'] = true;
I have a PHP array which looks like this example: $array[0][0] = 'apples'; $array[0][1]
I have a PHP array that looks like this: Array{ [0] { 'id' =>
Possible Duplicate: php sort array by sub-value I have a multidimensional array like the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.