[英]How to build json for two mysql queries?
this is the code i use to generate single json object 这是我用来生成单个json对象的代码
$SQL = mysql_query("SELECT * FROM `receipts` WHERE DATE(date) = '2011-08-03'");
if(mysql_num_rows($SQL ) > 0){
$i=0;
$responce->success = true;
while($SQL_RESULT = mysql_fetch_object($SQL)){
$responce->data[$i]['reciept_no'] = $SQL_RESULT->reciept_no;
$responce->data[$i]['time'] = $SQL_RESULT->date;
$responce->data[$i]['user'] = $SQL_RESULT->user;
$i++;
}
}
else{
$responce->success = false;
$responce->data = '';
$responce->reason = "No Activity...";
}
echo json_encode($responce);
result is like 结果就像
{"success":true,"data":[{"reciept_no":"2411","time":"09:33:56 AM","user":"test"},
{"reciept_no":"2412","time":"11:29:01 AM","user":" test "}]}
so there is another query which similar to this and generate exact same kind of output but from a another mysql table 所以有另一个类似于此的查询,并从另一个mysql表生成完全相同类型的输出
i want to do is combine two results and send to javascript then decode it in javascript 我想要做的是结合两个结果并发送到JavaScript,然后在JavaScript中解码它
like wrap first result with like table1 second result with table 2 or something 像第一个结果用table1第二个结果用表2或其他东西包装
how to do that? 怎么做?
Sorry for the bad English 对不起英语不好
Regards 问候
您可以运行两个查询,一个输出到$ response1,另一个输出到$ response2,然后您可以使用:
echo json_encode(array('table1'=>$response1,'table2'=>$response2));
JSON is just a text representation of a data structure. JSON只是数据结构的文本表示。 If you want to store two separate results in a single structure, then do
如果要在单个结构中存储两个单独的结果,请执行
data['response #1'] = 'blah blah blah';
data['response #2'] = 'other other other';
You could store the two queries' data into a single sub-array, but then you'd need some extra data to be able to differentiate betweeen the two data sources. 您可以将两个查询的数据存储到一个子数组中,但是您需要一些额外的数据才能区分两个数据源。 "did this record come from query #1? or from query #2?"
“这条记录是来自查询#1还是来自查询#2?”
$SQL = mysql_query("SELECT * FROM `receipts` WHERE DATE(date) = '2011-08-03'");
if(mysql_num_rows($SQL ) > 0){
$i=0;
while($SQL_RESULT = mysql_fetch_object($SQL)){
$responce->data[$i]['reciept_no'] = $SQL_RESULT->reciept_no;
$responce->data[$i]['time'] = $SQL_RESULT->date;
$responce->data[$i]['user'] = $SQL_RESULT->user;
$i++;
}
}
$SQL = mysql_query("SELECT * FROM `receipts2` WHERE DATE(date) = '2011-08-03'");
if(mysql_num_rows($SQL ) > 0){
$i=0;
$responce->success = true;
while($SQL_RESULT = mysql_fetch_object($SQL)){
$responce->data[$i]['reciept_no'] = $SQL_RESULT->reciept_no;
$responce->data[$i]['time'] = $SQL_RESULT->date;
$responce->data[$i]['user'] = $SQL_RESULT->user;
$i++;
}
}
else{
$responce->success = false;
$responce->data = '';
$responce->reason = "No Activity...";
}
echo json_encode($responce);
Is that your question? 那是你的问题吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.