如何在同一个php页面

时间:2018-05-16 07:28:25

标签: javascript php ajax

我想一起使用query和query2两个不同的选择查询。

如何使用并执行?

这是我的代码

$query2 = "SELECT advance FROM transaction WHERE adv_date=now()";
$query = "SELECT SUM(s.total) AS totalamount FROM sale s JOIN product p ON ( s.pr_id = p.pr_id ) WHERE s.customer_name = '$customer'";

$result = mysqli_query($connect, $query);
$result2 = mysqli_query($connect, $query2);

$output = '';

if (mysqli_num_rows($result) > 0) {
    $i = 0;
    while ($row = mysqli_fetch_array($result)) {
        $output .= '<h4 style="display: inline;" align="right"> ' . $row["totalamount"] . '</h4><br> ';
        $output .= ' ';
        $i++;
    }
} else {
    $output .= '<tr>
      <td colspan="5">No Order Found</td>
    </tr>';
}
$output .= '<h4 style="display:inline;" align="right"> ' . $row["total"] . '</h4><br> ';
echo $output; ?>

2 个答案:

答案 0 :(得分:0)

试试这个它会帮助你

$sql = "SELECT Lastname FROM Persons ORDER BY LastName;";
$sql .= "SELECT Country FROM Customers";

// 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\n",$row[0]);
        }
      // Free result set
      mysqli_free_result($result);
      }
    }
  while (mysqli_next_result($con));
}

答案 1 :(得分:0)

我不确定我是否理解你的问题,但我创建了一个函数来执行这两个查询,方法是将它们传递给函数的参数。

function commitQuery($query){
 $result = mysqli_query($connect, $query);    
 $output = '';  
 if(mysqli_num_rows($result) > 0) {  
  $i=0;

  while($row = mysqli_fetch_array($result)) { 
    $output .= '<h4 style="display:inline;" align="right"> '. $row["totalamount"] .'</h4><br>';  
    $output .= '';  
    $i++;
  }  
 } else {  
    $output .= '  
     <tr> 
      <td colspan="5">No Order Found</td>  
     </tr>';  
 }  

    $output .= '<h4 style="display:inline;" align="right"> '. $row["total"] .'</h4><br>';  

   echo $output;  
 }
}

commitQuery("SELECT advance FROM transaction WHERE adv_date=now()");
commitQuery("SELECT SUM(s.total) AS totalamount FROM sale s JOIN product p ON ( s.pr_id = p.pr_id ) WHERE s.customer_name = '$customer'");