【问题标题】:Saving JSON string to MySQL database将 JSON 字符串保存到 MySQL 数据库
【发布时间】:2012-07-04 11:14:27
【问题描述】:

我有一个 JSON 字符串

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

我需要将它保存在数据库中。我怎么能这样做?

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

    ?>

【问题讨论】:

    标签: php mysql xampp


    【解决方案1】:

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

    假设 $json 是 {"name":"jack","school":"colorado state","city":"NJ","id":null}

    $json_array = json_decode($json);
    

    您现在在 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());  
    

    【讨论】:

    • 在我的 Firebug 控制台中,我发布了{"name":"jack","school":"colorado state","city":"NJ","id":null},那么如何获取该值并将其保存到$json,如您的代码所示?
    • 逃跑是什么意思。对不起,我是 PHP 新手
    • 我如何接收 JSON 字符串到$json
    • 我以为你已经有了它,如果你说它显示在你的 Firebug 控制台上,它是从哪里显示的?谁创造了它?请解释一下,我可以帮你把它变成$json。
    • 如果你的 json 信息来自表单怎么办?你会如何编码?
    【解决方案2】:

    我们将使用json_decode json_decode documentation

    也一定要逃跑!下面是我的做法...

    /* 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();
    

    希望这会有所帮助!

    【讨论】:

    • 我不明白jsonGiven 是什么。我的 JSON 字符串是 {"name":"jack","school":"colorado state","city":"NJ","id":null}。而且我无法从我的 PHP 代码中读取它。
    • jsonGiven 将是示例参数名称,其中值是您的 json 字符串。一个例子是example.com/thisScript.php?jsonGiven={"name":"jack",etc..}
    【解决方案3】:

    这是帮助您的示例

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

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2018-09-21
      • 2011-10-14
      • 1970-01-01
      • 2011-09-30
      • 2019-12-21
      • 2023-03-18
      • 1970-01-01
      • 2016-09-06
      • 2015-02-07
      相关资源
      最近更新 更多