<tfoot id='QOilU'></tfoot>

<small id='QOilU'></small><noframes id='QOilU'>

    • <bdo id='QOilU'></bdo><ul id='QOilU'></ul>

      <i id='QOilU'><tr id='QOilU'><dt id='QOilU'><q id='QOilU'><span id='QOilU'><b id='QOilU'><form id='QOilU'><ins id='QOilU'></ins><ul id='QOilU'></ul><sub id='QOilU'></sub></form><legend id='QOilU'></legend><bdo id='QOilU'><pre id='QOilU'><center id='QOilU'></center></pre></bdo></b><th id='QOilU'></th></span></q></dt></tr></i><div id='QOilU'><tfoot id='QOilU'></tfoot><dl id='QOilU'><fieldset id='QOilU'></fieldset></dl></div>
    1. <legend id='QOilU'><style id='QOilU'><dir id='QOilU'><q id='QOilU'></q></dir></style></legend>
      1. 将 JSON 字符串保存到 MySQL 数据库

        时间:2024-04-12
          <tbody id='1CcfL'></tbody>
      2. <i id='1CcfL'><tr id='1CcfL'><dt id='1CcfL'><q id='1CcfL'><span id='1CcfL'><b id='1CcfL'><form id='1CcfL'><ins id='1CcfL'></ins><ul id='1CcfL'></ul><sub id='1CcfL'></sub></form><legend id='1CcfL'></legend><bdo id='1CcfL'><pre id='1CcfL'><center id='1CcfL'></center></pre></bdo></b><th id='1CcfL'></th></span></q></dt></tr></i><div id='1CcfL'><tfoot id='1CcfL'></tfoot><dl id='1CcfL'><fieldset id='1CcfL'></fieldset></dl></div>

            <small id='1CcfL'></small><noframes id='1CcfL'>

            • <legend id='1CcfL'><style id='1CcfL'><dir id='1CcfL'><q id='1CcfL'></q></dir></style></legend>
              • <tfoot id='1CcfL'></tfoot>

                  <bdo id='1CcfL'></bdo><ul id='1CcfL'></ul>
                  本文介绍了将 JSON 字符串保存到 MySQL 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 JSON 字符串

                  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 ?

                  我的 PHP 代码(我只建立了与 MySQL 的连接,但我无法保存记录)

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

                  推荐答案

                  我们将使用 json_decode json_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
                  ", 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();
                  

                  希望这会有所帮助!

                  这篇关于将 JSON 字符串保存到 MySQL 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PhpStorm 代码完成不显示核心类/扩展 下一篇:XAMPP - 未配置会话缓存 [提示:SSLSessionCache]

                  相关文章

                    <tfoot id='M8GEM'></tfoot>
                    <legend id='M8GEM'><style id='M8GEM'><dir id='M8GEM'><q id='M8GEM'></q></dir></style></legend>

                    <i id='M8GEM'><tr id='M8GEM'><dt id='M8GEM'><q id='M8GEM'><span id='M8GEM'><b id='M8GEM'><form id='M8GEM'><ins id='M8GEM'></ins><ul id='M8GEM'></ul><sub id='M8GEM'></sub></form><legend id='M8GEM'></legend><bdo id='M8GEM'><pre id='M8GEM'><center id='M8GEM'></center></pre></bdo></b><th id='M8GEM'></th></span></q></dt></tr></i><div id='M8GEM'><tfoot id='M8GEM'></tfoot><dl id='M8GEM'><fieldset id='M8GEM'></fieldset></dl></div>

                        <bdo id='M8GEM'></bdo><ul id='M8GEM'></ul>

                      <small id='M8GEM'></small><noframes id='M8GEM'>