在php中运行两个选择查询并以json格式对其进行编码?

时间:2016-04-28 07:15:35

标签: php mysql json

我有两个表billing_all和payment_details。我正在尝试编写PHP代码,以便我可以在相同的PHP代码中运行2个查询并以json格式编码它们的数据。我目前正在使用以下代码来实现没有json编码: -

children: [{
        "xtype": "checkbox",// remove
        "checked":"false", //remove
        "expanded": "false",
        "leaf": "true",
        "qtip": "Make Copy",
        "text": "Make Copy",
        "disabled": "true",
        "cls" : "x-hidden-node"
      }]

如何以json格式编码接收的数据?我尝试过类似的东西: -

<?php
        include "config.php";
        $dbname ="webappdb";
        $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$dbname);
         if(!$con)
         {
           echo "Connection Error".mysqli_connect_error();
         }
         else{
        //echo "";
         }
$sql = "SELECT SUM(total_wt) FROM `billing_all` WHERE date ='15-Apr-2016';";
$sql .= "SELECT pymt_amount, mode_of_pymt FROM  `payment_details` WHERE DATE ='15-Apr-2016';";
// Execute multi query
if (mysqli_multi_query($con,$sql))
{
  do
    {
    // Store first result set
    if ($result=mysqli_store_result($con)) {
      // Fetch one and one row
      while ($row=mysqli_fetch_row($result))
        {
        printf("%s%s\n",$row[0],$row[1]);
        }
      // Free result set
      mysqli_free_result($result);
      }
    }
  while (mysqli_next_result($con));
}
mysqli_close($con);
?>
  

哪个没有给我预期的结果。如何编码数据   以正确的方式?以下是2个表的结构

enter image description here enter image description here

  

我是编程新手,感谢任何帮助。谢谢你?

1 个答案:

答案 0 :(得分:1)

<?php

//Joining both tables on the basis of doc_no column

 $sql = "SELECT billing_all.SUM(total_wt) AS total
        ,payment_details.pymt_amount
        ,payment_details.mode_of_pymt
    FROM billing_all
        ,payment_details
    WHERE billing_all.doc_no = payment_details.doc_no
        AND billing_all.DATE = '15-Apr-2016'";


// Executing query
$res = mysqli_query($con,$sql); 

//initializing array to store result
$rows = array(); 

//Iterating result and storing each row in $r then array $rows to covert result set into array because json accept array as parameter.

while($r = mysqli_fetch_assoc($res)) 
{
    $rows[] = $r; 
}

//print whole array in json format
echo  json_encode($rows); ?>