8

I have a JSON string with me

{"name":"jack","school":"colorado state","city":"NJ","id":null}

I need it to be saved in the Database. How could i do this ?

My PHP code (I have only establish the connection to MySQL, but i am unable to save the records)

   <?php
    // the MySQL Connection
    mysql_connect("localhost", "username", "pwd") or die(mysql_error());
    mysql_select_db("studentdatabase") or die(mysql_error());

    // Insert statement

    mysql_query("INSERT INTO student
    (name, school,city) VALUES(------------------------- ) ") // (How to write this)
    or die(mysql_error());  


    echo "Data Inserted or failed";

    ?>

3 Answers 3

17

We'll use json_decode json_decode documentation

Also be sure to escape! here's how I would do it below...

/* create a connection */
$mysqli = new mysqli("localhost", "root", null, "yourDatabase");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* let's say we're grabbing this from an HTTP GET or HTTP POST variable called jsonGiven... */
$jsonString = $_REQUEST['jsonGiven'];
/* but for the sake of an example let's just set the string here */
$jsonString = '{"name":"jack","school":"colorado state","city":"NJ","id":null}
';

/* use json_decode to create an array from json */
$jsonArray = json_decode($jsonString, true);

/* create a prepared statement */
if ($stmt = $mysqli->prepare('INSERT INTO test131 (name, school, city, id) VALUES (?,?,?,?)')) {

    /* bind parameters for markers */
    $stmt->bind_param("ssss", $jsonArray['name'], $jsonArray['school'], $jsonArray['city'], $jsonArray['id']);

    /* execute query */
    $stmt->execute();

    /* close statement */
    $stmt->close();
}

/* close connection */
$mysqli->close();

Hope this helps!

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

2 Comments

i don't understand what jsonGiven is. My JSON string is this {"name":"jack","school":"colorado state","city":"NJ","id":null}. And i am unable to read it from my PHP code.
jsonGiven would be the example parameter name where the value is your json string. An example would be example.com/thisScript.php?jsonGiven={"name":"jack",etc..}
1

This is example for help you

<?php
 $json = '{"name":"jack","school":"colorado state","city":"NJ","id":null}';// You can get it from database,or Request parameter like $_GET,$_POST or $_REQUEST or something :p
 $json_array = json_decode($json);

 echo $json_array["name"];
 echo $json_array["school"];
 echo $json_array["city"];
 echo $json_array["id"];
?>

Hope this help !

Comments

0

Decode into an array and pass it in your mysql_query, the code below is not using mysql_real_escape_string or any other means of security, which you should implement.

Assume $json is {"name":"jack","school":"colorado state","city":"NJ","id":null}

$json_array = json_decode($json);

You now have indexes in a php array, such as: $json_array['name']

mysql_query("INSERT INTO student (name, school,city) VALUES('".$json_array['name']."', '".$json_array['school']."', '".$json_array['city']."') ") or die(mysql_error());  

5 Comments

In my Firebug console, I am posting {"name":"jack","school":"colorado state","city":"NJ","id":null} so how do i fetch this value and save it to $json as shown in your code ?
what do you mean by escape. sorry i am new to PHP
How do i receive the JSON string to $json ?
I thought you already had it, if you're saying it shows on your Firebug console, it shows up from where? Who creates it? Please explain, so I can help you turn it into the $json.
What if your json information comes in from a form? How would you encode it?

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.