Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving JSON string to MySQL database

Tags:

php

mysql

xampp

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";

    ?>
like image 990
Sharon Watinsan Avatar asked Jul 04 '12 00:07

Sharon Watinsan


People also ask

Can I store JSON as string in MySQL?

MySQL supports a native JSON data type defined by RFC 7159 that enables efficient access to data in JSON (JavaScript Object Notation) documents. The JSON data type provides these advantages over storing JSON-format strings in a string column: Automatic validation of JSON documents stored in JSON columns.

Can you save JSON in MySQL?

MySQL allows you to store JSON data in native JSON data type since version 5.7. 8. This enables you to store JSON documents easily, and extract JSON data elements without data conversion.

Can I save JSON to database?

SQL Server and Azure SQL Database have native JSON functions that enable you to parse JSON documents using standard SQL language. You can store JSON documents in SQL Server or SQL Database and query JSON data as in a NoSQL database.

Can MySQL read JSON data?

MySQL supports a native JSON data type that supports automatic validation and optimized storage and access of the JSON documents. Although JSON data should preferably be stored in a NoSQL database such as MongoDB, you may still encounter tables with JSON data from time to time.


2 Answers

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!

like image 81
sevenadrian Avatar answered Sep 18 '22 21:09

sevenadrian


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 !

like image 21
Objectoop Oop Avatar answered Sep 17 '22 21:09

Objectoop Oop