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

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

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

        PHP读取数据库记录转换为JSON的代码

        时间:2024-05-12
      2. <legend id='Lh5G8'><style id='Lh5G8'><dir id='Lh5G8'><q id='Lh5G8'></q></dir></style></legend>

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

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

                <tbody id='Lh5G8'></tbody>
              • <small id='Lh5G8'></small><noframes id='Lh5G8'>

                  为了提供API接口,我们常常在读取数据库后,将数据转换为数组,通过json_encode转为JSON,即可满足使用需要。现将代码粘帖如下:

                  1. 读取一条记录,转为数组并输出JSON
                  include("../../db/conn.php");//数据库连接;
                  echo "<pre>";
                  //数据库读取后,直接转换为数组显示;
                  $sql = "select salesid,fromstore,fromsaler,salestime,salenum,totalprice from midea_sales WHERE salesid=44";
                  $results = mysqli_query($con, $sql);
                  $rows = mysqli_fetch_assoc($results);
                  foreach ($rows as $key => $v) {
                  	$res[$key] = $v;
                  }
                  echo json_encode($res);
                  
                  1. 读取N条记录,转为多维数组并输出JSON(第一种写法)
                  //数据库读取后,直接转换为数组显示;
                  $sql = "select salesid,fromstore,fromsaler,salestime,salenum,totalprice from midea_sales";
                  $results = mysqli_query($con, $sql);
                  
                  $data = array();//初始化数组;
                  
                  class Alteration
                  {
                  	public $fromstore;
                  	public $fromsaler;
                  	public $salenum;
                  	public $totalprice;
                  }
                  
                  while ($row = mysqli_fetch_assoc($results)) {
                  	$alter = new Alteration();//实例化对象;
                  	$alter->fromstore = $row['fromstore'];
                  	$alter->fromsaler = $row['fromsaler'];
                  	$alter->salenum = $row['salenum'];
                  	$alter->totalprice = $row['totalprice'];
                  	$data[] = $alter;
                  }
                  echo json_encode($data);
                  
                  1. 读取N条记录,转为多维数组并输出JSON(第二种写法)
                  $sql = "select salesid,fromstore,fromsaler,salestime,salenum,totalprice from midea_sales";
                  $results = mysqli_query($con, $sql);
                  while ($rows = mysqli_fetch_assoc($results)) {
                  	$res[] = $rows;
                  }
                  //$res = str_replace('[', '{', json_encode($res));
                  //$res = str_replace(']', '}', $res);
                  print_r($res);
                  

                  4.读取N条记录,转为多维数组并输出JSON(第三种写法),适合获取全部记录

                  $sql = "select salesid,fromstore,fromsaler,salestime,salenum,totalprice from midea_sales";
                  $results = mysqli_query($con, $sql);
                  $rows = mysqli_fetch_all($results);
                  print_r($rows);
                  

                  在转换的过程中,JSON格式会出现[]和{}两种格式的JSON,而实际应用中对{}的接口是标准接口。如何转换呢?
                  原因在于:当array是一个从0开始的连续数组时,json_encode出来的结果是一个由[]括起来的字符串;而当array是不从0开始或者不连续的数组时,json_encode出来的结果是一个由{}括起来的key-value模式的字符串。

                  $sql = "select salesid,fromstore,fromsaler,salestime,salenum,totalprice from midea_sales";
                  $results = mysqli_query($con, $sql);
                  $rows = mysqli_fetch_all($results);
                  $rows = str_replace('[', '{', json_encode($rows));
                  $rows = str_replace(']', '}', $rows);
                  echo json_encode($rows);
                  上一篇:php从数据库取数据转换成字符串或json格式 下一篇:没有了

                  相关文章

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

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

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

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