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 6628861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:10:40+00:00 2026-05-25T22:10:40+00:00

php how to store and read json data via mysql? mysql_query(INSERT INTO text (data)

  • 0

php how to store and read json data via mysql?

mysql_query("INSERT INTO text (data) VALUES (json_encode('id' => $uid, 'value' => yes))");

then, how to update data value? read out the data, then insert it with an json_encode and decode process, or only easy way update?

[{"id": "1", "value": "yes"}]

then insert another change to [{"id": "1", "value": "yes"},{"id": "2", "value": "yes"}]…

Or even if a long long value.

[{"id": "1", "value": "yes"},{"id": "2", "value": "yes"}...{"id": "10000", "value": "yes"}]

then update another one, change to [{"id": "1", "value": "yes"},{"id": "2", "value": "yes"}...{"id": "10000", "value": "yes"},{"id": "10001", "value": "yes"}]

I wanna to ask, how to do this mysql query processing more wiser and efficiently? Thanks for more suggestion.

  • 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-05-25T22:10:41+00:00Added an answer on May 25, 2026 at 10:10 pm

    Technically, you are going the wrong way with that. MySQL is used to store each of your ID/VALUE seperately. For the sake of NOT changing your code we’ll look at your solution first but then i’ll explain the "better" way of doing it.

    First, you need to make your JSON as a variable, not part of your SQL:

    mysql_query("INSERT INTO text (data) VALUES (".mysql_real_escape_string(array(json_encode('id' => $uid, 'value' => 'yes'))).")");

    instead of

    mysql_query("INSERT INTO text (data) VALUES (json_encode('id' => $uid, 'value' => yes))");

    This first part will allow you to at least instead the data correctly into mysql. I am ASSUMING your table has an ID and that you will be using it to update or delete

    When you retrieve your data, you can json_decode the $row['data'] to get your data back from the row and work with it. To update it, just do:

    mysql_query("UPDATE text SET data = "'.mysql_real_escape_string(json_encode($myJsonToBeData)).'" WHERE rowid = '.$myrowid)
    

    Now, for the RIGHT way to do this:

    The right way to do this would be to have these fields into your table: ID, JSONID, JSONVALUE and use this SQL instead:

    SELECT * FROM text WHERE id = $rowid
    INSERT INTO text VALUES(NULL, $jsonid, $jsonvalue)
    UPDATE text SET jsonid = $jsonid, jsondata = $jsondata
    

    This is pretty basic, but it will allow you to have any number of entries in your database that make it searchable, indexed, sortable, queryable, etc…

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

Sidebar

Related Questions

I try to store the PHP floating point value 63.59072952118762 into a double precision
I have to return data fetched from MySQL table into a php file as
I want to call a JavaScript function through PHP and store the returned value
Does it possible to execute php file and store its output into some variable?
The Store var timesheet = new Ext.data.JsonStore( { root: 'timesheetEntries', url: 'php/scripts/timecardEntry.script.php', storeId: 'timesheet',
I would like to store images in my mySQL database using the CodeIgniter PHP
I need to be able to store data for a php application in a
I'm creating a website and new to php and mysql. I just read as
How can I read strings like that? What do they mean? a:10:{i:0;s:7:default;i:1;s:8:failsafe;i:2;s:4:foaf;i:3;s:4:ical;i:4;s:2:js;i:5;s:4:json;i:6;s:6:opendd;i:7;s:3:php;i:8;s:3:rss;i:9;s:3:xml;} I've seen
Hi i am using PHP to store the timezone.The timezone coming from dropdown and

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.