Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have an Object from Javascript pass to PHP (via AJAX):

var jsObject = {
    "name": "Michael Divo",
    "age": 27,
    "country": "United States"
};

jsObject_json = JSON.stringify( jsObject );

$.ajax({
    type: "POST",
    dataType: 'json',
    url: "http://www.example.com/myserver.php",
    data: { mydata: jsObject_json },
}) ...

In, myserver.php:

$json = json_decode($_POST["mydata"], true);

Then if i save that $json into the MySQL from this PHP end, it is saved as:

Array

.. in the Database, as that "Array" as a String.


So how do i properly SAVE this JSON into the MySQL please? (So that i can later retrieve it back as a JSON and read.)

share|improve this question
    
USe serialize function to store into DB. – Jitendra Yadav Jun 12 '14 at 12:30
up vote 1 down vote accepted

You dont need to decode it before you store it into db, (store it as pure string), then if you what to get it back(somewhere else in your code) get that string and decode it.

share|improve this answer

If you just want to pull it back as JSON, then don't run it through json_decode. Leave it as a string.

Most sensible systems, however, would have a database structure that would allow each of the fields in the JSON submission to be handled separately. Then you would access each field and add it in its own column in the INSERT query. You could then perform queries on the data beyond "Give me all the data" (e.g. WHERE country = 'United States').

$preparedStatement = $db->prepare('INSERT INTO data (name, age, country) VALUES (:name, :age, :country)');
$preparedStatement->execute(json_decode($_POST["mydata"], true));
share|improve this answer

By using json_encode() you can reencode your Array into a JSON that can be stored into a TEXT field in MySQL. Or since, you already have it as a string, leave it as is.

$stmt = $db->prepare("INSERT INTO FOO (mydata) VALUES (:my_data)");
$stmt->execute($_POST);
share|improve this answer

As your Database schema is not provided here so assuming that you need to store the string only, in this case don't decode your json as this will turn json string to array. In case of you have schema then you can proceed by decoding this into array and then save elements according to you table schema.

share|improve this answer

try this

$str = $db->prepare("INSERT INTO table (json_array) VALUES (:json_data)");
$str->execute($_POST["mydata"]);
share|improve this answer
4  
Danger: You are vulnerable to SQL injection attacks that you need to defend yourself from. – Quentin Jun 12 '14 at 12:25
    
@Quentin now every thing is ok – Gabber Jun 12 '14 at 12:31
    
Except for not actually putting any placeholders in your query – Quentin Jun 12 '14 at 12:34
    
sorry @Quentin i am not php developer,so i have no idea about PHP,but i edit again my answer.if something wrong in my answer so plz correct it. – Gabber Jun 12 '14 at 12:41
2  
And now you're vulnerable to SQL injection attacks again. – Quentin Jun 12 '14 at 12:45

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.