php 将 JSON 字符串保存到 MySQL 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11320796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:16:30  来源:igfitidea点击:

Saving JSON string to MySQL database

phpmysqlxampp

提问by sharon Hwk

I have a JSON string with me

我有一个 JSON 字符串

{"name":"Hyman","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 代码(我只建立了到 MySQL 的连接,但我无法保存记录)

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

    ?>

回答by sevenadrian

We'll use json_decodejson_decode documentation

我们将使用json_decodejson_decode 文档

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":"Hyman","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!

希望这可以帮助!

回答by Objectoop Oop

This is example for help you

这是帮助您的示例

<?php
 $json = '{"name":"Hyman","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 !

希望这有帮助!

回答by André Catita

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.

解码成一个数组并将其传递到您的 mysql_query 中,下面的代码没有使用 mysql_real_escape_string 或任何其他您应该实现的安全手段。

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

假设 $json 是 {"name":"Hyman","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']

您现在在 php 数组中有索引,例如: $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());