• <small id='HnaZw'></small><noframes id='HnaZw'>

        <bdo id='HnaZw'></bdo><ul id='HnaZw'></ul>
      <tfoot id='HnaZw'></tfoot>
      1. <legend id='HnaZw'><style id='HnaZw'><dir id='HnaZw'><q id='HnaZw'></q></dir></style></legend>

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

        如何将 mysqli 结果转换为 JSON?

        时间:2023-07-31
        1. <small id='k8w1n'></small><noframes id='k8w1n'>

          • <bdo id='k8w1n'></bdo><ul id='k8w1n'></ul>
            <tfoot id='k8w1n'></tfoot>

                <i id='k8w1n'><tr id='k8w1n'><dt id='k8w1n'><q id='k8w1n'><span id='k8w1n'><b id='k8w1n'><form id='k8w1n'><ins id='k8w1n'></ins><ul id='k8w1n'></ul><sub id='k8w1n'></sub></form><legend id='k8w1n'></legend><bdo id='k8w1n'><pre id='k8w1n'><center id='k8w1n'></center></pre></bdo></b><th id='k8w1n'></th></span></q></dt></tr></i><div id='k8w1n'><tfoot id='k8w1n'></tfoot><dl id='k8w1n'><fieldset id='k8w1n'></fieldset></dl></div>
                  <tbody id='k8w1n'></tbody>
                  <legend id='k8w1n'><style id='k8w1n'><dir id='k8w1n'><q id='k8w1n'></q></dir></style></legend>
                • 本文介绍了如何将 mysqli 结果转换为 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个 mysqli 查询,我需要将其格式化为移动应用程序的 JSON.

                  我已经设法为查询结果生成了一个 XML 文档,但是我正在寻找更轻量级的东西.(请参阅下面我当前的 XML 代码)

                  $mysql = new mysqli(DB_SERVER,DB_USER,DB_PASSWORD,DB_NAME) or die('连接数据库时出现问题');$stmt = $mysql->prepare('SELECT DISTINCT title FROM Section ORDER BY title ASC');$stmt->execute();$stmt->bind_result($title);//创建xml格式$doc = new DomDocument('1.0');//创建根节点$root = $doc->createElement('xml');$root = $doc->appendChild($root);//为每一行添加节点while($row = $stmt->fetch()) :$occ = $doc->createElement('data');$occ = $root->appendChild($occ);$child = $doc->createElement('section');$child = $occ->appendChild($child);$value = $doc->createTextNode($title);$value = $child->appendChild($value);终了;$xml_string = $doc->saveXML();header('Content-Type: application/xml; charset=ISO-8859-1');//输出 xml jQuery 就绪回声 $xml_string;

                  解决方案

                  $mysqli = new mysqli('localhost','user','password','myDatabaseName');$myArray = array();if ($result = $mysqli->query("SELECT * FROM phase1")) {while($row = $result->fetch_array(MYSQLI_ASSOC)) {$myArray[] = $row;}回声 json_encode($myArray);}$result->close();$mysqli->close();

                  1. $row = $result->fetch_array(MYSQLI_ASSOC)
                  2. $myArray[] = $row

                  输出如下:

                  <预><代码>[{"id":"31","name":"pruduct_name1","price":"98"},{"id":"30","name":"pruduct_name2","price":"23"}]

                  如果你想要另一种风格,你可以试试这个:

                  1. $row = $result->fetch_row()
                  2. $myArray[] = $row

                  输出如下:

                  <预><代码>[["31","pruduct_name1","98"],["30","pruduct_name2","23"]]

                  I have a mysqli query which I need to format as JSON for a mobile application.

                  I have managed to produce an XML document for the query results, however I am looking for something more lightweight. (See below for my current XML code)

                  $mysql = new mysqli(DB_SERVER,DB_USER,DB_PASSWORD,DB_NAME) or die('There was a problem connecting to the database');
                  
                  $stmt = $mysql->prepare('SELECT DISTINCT title FROM sections ORDER BY title ASC');
                  $stmt->execute();
                  $stmt->bind_result($title);
                  
                  // create xml format
                  $doc = new DomDocument('1.0');
                  
                  // create root node
                  $root = $doc->createElement('xml');
                  $root = $doc->appendChild($root);
                  
                  // add node for each row
                  while($row = $stmt->fetch()) : 
                  
                      $occ = $doc->createElement('data');  
                      $occ = $root->appendChild($occ);  
                  
                      $child = $doc->createElement('section');  
                      $child = $occ->appendChild($child);  
                      $value = $doc->createTextNode($title);  
                      $value = $child->appendChild($value);  
                  
                  endwhile;
                  
                  $xml_string = $doc->saveXML();  
                  
                  header('Content-Type: application/xml; charset=ISO-8859-1');
                  
                  // output xml jQuery ready
                  
                  echo $xml_string;
                  

                  解决方案

                  $mysqli = new mysqli('localhost','user','password','myDatabaseName');
                  $myArray = array();
                  if ($result = $mysqli->query("SELECT * FROM phase1")) {
                  
                      while($row = $result->fetch_array(MYSQLI_ASSOC)) {
                              $myArray[] = $row;
                      }
                      echo json_encode($myArray);
                  }
                  
                  $result->close();
                  $mysqli->close();
                  

                  1. $row = $result->fetch_array(MYSQLI_ASSOC)
                  2. $myArray[] = $row

                  output like this:

                  [
                      {"id":"31","name":"pruduct_name1","price":"98"},
                      {"id":"30","name":"pruduct_name2","price":"23"}
                  ]
                  

                  If you want another style, you can try this:

                  1. $row = $result->fetch_row()
                  2. $myArray[] = $row

                  output will like this:

                  [
                      ["31","pruduct_name1","98"],
                      ["30","pruduct_name2","23"]
                  ]
                  

                  这篇关于如何将 mysqli 结果转换为 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:MySQLi 查询只返回一行 下一篇:如何将我的站点从 mysql 迁移到 mysqli?

                  相关文章

                  最新文章

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

                  1. <small id='kt3jv'></small><noframes id='kt3jv'>

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