简体   繁体   English

使用PHP的SQL查询从AJAX返回多个结果

[英]Return multiple results with AJAX from sql query in php

Hello I am realizing simple AJAX request and would like to be able to store the results from the SQL SELECT query into 3 different ajax variables. 您好,我正在实现简单的AJAX请求,并希望能够将来自SQL SELECT查询的结果存储到3个不同的ajax变量中。

Where 2 of them will store one variable and the other one have to store foreach results. 其中两个将存储一个变量,另一个必须存储foreach结果。

Let's say my AJAX request is the following: 假设我的AJAX请求如下:

$.post('includes/check_number.php', {'date':date, 'userid':userid}, function(data) {
              $("#time-result").html(data.result01);
              $("#time-sum-result").html(data.result02);

Where I will have 2 results result01 and result02 我将在其中得到2个结果result01和result02

In the current state of my script inside the mysql select request what is returning like data is the following: 在mysql select请求中我脚本的当前状态下,返回的data如下:

        $stmt = $con->prepare( $sql );                      
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $stmt->execute();       
foreach($stmt as $row) {
            echo "<tr>
                     <td>".$row['clientname']."</td>   
                     <td>".$row['taskname']."</td>
                     <td>".$row['department']."</td>
                     <td>".$row['note']."</td>
                     <td>".$row['caseid']."</td>
                     <td>".$row['time']."</td>                                                                        
                    </tr>";                     
            }

I would like to put the result of the forreach as it is inside the echo, where it will contains various iterations and then for result02 for example would like to put only one row of the same query for example like: $row['date'] 我想将forreach的结果放入回显中,其中将包含各种迭代,然后对于result02例如,只想将同一查询的一行放入例如: $row['date']

In this case 在这种情况下

data.result01 - will have all the code of the <tr></tr> data.result02 - will have only one variable which is date. data.result01-将具有<tr></tr>所有代码data.result02-将仅具有一个变量,即date。

Question is how to dump the foreach into result01 and in the same time to put in result02 only one row from the same query. 问题是如何将foreach转储到result01,同时又将来自同一查询的一行仅放入result02。 $stmt

Export all your data first then use it with jquery ? 首先导出所有数据,然后将其与jquery一起使用? Something like : 就像是 :

PHP : PHP的:

foreach($stmt as $row) {
   $arr_out[] = $row;
}
echo json_encode($arr_out);
exit();

JQUERY : JQUERY:

var result1 = "";

$.post('includes/check_number.php', {'date':date, 'userid':userid}, function(data) {
  $.each(data, function(key, item) {
     result1 += "<tr><td>"+item.clientname+"</td>[...]<td>"+item.time+"</td></tr>";
     result2 = item.date;
  });
  $("#time-result").html(result1);
}

I didn't test this code, hope it will help you. 我没有测试此代码,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM