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

      <legend id='Q0XVS'><style id='Q0XVS'><dir id='Q0XVS'><q id='Q0XVS'></q></dir></style></legend>

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

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

    1. 如何正确使用 PHP 将 MySQL 对象编码为 JSON?

      时间:2023-07-29
    2. <tfoot id='Ccn6D'></tfoot>

      • <bdo id='Ccn6D'></bdo><ul id='Ccn6D'></ul>
          <tbody id='Ccn6D'></tbody>

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

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

              1. 本文介绍了如何正确使用 PHP 将 MySQL 对象编码为 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试遍历 MySQL 对象并在另一个页面上使用 ajax 调用来附加数据,但我无法让 php 向回调返回有效的 JSON.

                这个显然行不通...

                query($myQuery) or die($mysqli->error);$row = $result->fetch_assoc();回声 json_encode($row);?>

                或者这个...

                query($myQuery) or die($mysqli->error);while ( $row = $result->fetch_assoc() ){回声 json_encode($row) .", ";}?>

                解决方案

                $data = array();while ( $row = $result->fetch_assoc() ){$data[] = json_encode($row);}回声 json_encode( $data );

                这应该可以.此外,您可以使用 http://jsonlint.com/ 查看您的 JSON 输出有什么问题.>

                更新:使用 fetch_all() 也可能是个好主意

                $data = $result->fetch_all( MYSQLI_ASSOC );回声 json_encode( $data );

                I am trying to iterate through a MySQL object and use an ajax call on another page to append the data but I can't get the php to return valid JSON to the callback.

                This one obviously doesn't work...

                <?php
                
                    $db_host = "localhost";
                    $db_user = "blah";
                    $db_pass = "blah";
                    $db_name = "chat";
                    $mysqli = new MySQLi($db_host, $db_user, $db_pass, $db_name);
                    $myQuery = "SELECT * FROM users";
                    $result = $mysqli->query($myQuery) or die($mysqli->error);
                    $row = $result->fetch_assoc();
                    echo json_encode($row);
                
                ?>
                

                Or this one...

                <?php
                
                    $db_host = "localhost";
                    $db_user = "blah";
                    $db_pass = "blah";
                    $db_name = "chat";
                    $mysqli = new MySQLi($db_host, $db_user, $db_pass, $db_name);
                    $myQuery = "SELECT * FROM users";
                    $result = $mysqli->query($myQuery) or die($mysqli->error);
                    while ( $row = $result->fetch_assoc() ){
                        echo json_encode($row) . ", ";
                    }
                
                ?>
                

                解决方案

                $data = array();
                
                while ( $row = $result->fetch_assoc() ){
                    $data[] = json_encode($row);
                }
                echo json_encode( $data );
                

                This should do it. Also, you can use http://jsonlint.com/ to see what are the problems with your JSON output.

                Update: using fetch_all() might be a good idea too

                $data = $result->fetch_all( MYSQLI_ASSOC );
                echo json_encode( $data );
                

                这篇关于如何正确使用 PHP 将 MySQL 对象编码为 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:使用准备好的语句编写查询的分页结果集, 下一篇:PHP mysqli 为没有参数的存储过程准备的语句

                相关文章

                最新文章

                1. <small id='70DlZ'></small><noframes id='70DlZ'>

                    <bdo id='70DlZ'></bdo><ul id='70DlZ'></ul>
                  <legend id='70DlZ'><style id='70DlZ'><dir id='70DlZ'><q id='70DlZ'></q></dir></style></legend>

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