[英]How to roam in SQL query more than one time in PHP
A friend of mine has asked me a question which i do not know how. 我的一个朋友问我一个问题,我不知道该怎么做。
The problem is he wants to use a result set of a query more than one time. 问题是他想多次使用查询结果集。 Whenever he wants. 每当他想要的时候。
There is example tables and example output attached. 附带示例表和示例输出。
I will query two times only: 我只会查询两次:
Select * from ornek1_ust
Select * from ornek1_alt
Is it possible to roam in a result set we already have with PHP to have some output like example output. 是否可以漫游到我们已经在PHP中获得的结果集中以具有某些输出,例如示例输出。 I want to query database with full data for once. 我想一次查询完整数据的数据库。 Then i want to use it wherever i want whenever i want. 然后,我想随时随地使用它。
Example Tables: 表格示例:
Example Output: 示例输出:
It sounds like you want to be using mysql_result() to be able to pull out data from a resultset at will. 听起来您想使用mysql_result()能够随意从结果集中提取数据。
If you want to loop through the whole dataset and still have it available to loop through again, you can use mysql_data_seek() to set the internal pointer back to the beginning. 如果要遍历整个数据集并且仍然可以再次遍历,则可以使用mysql_data_seek()将内部指针设置回开始。
Just store the query results in an array.... 只需将查询结果存储在数组中即可。...
$sql = "Select * from ornek1_ust";
$results = array();
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
$results[] = $row;
}
// Use $results further on in your code
You'd want to "cache" the results within PHP. 您想在PHP中“缓存”结果。 To fetch the data, you'd do a simple join query: 要获取数据,您可以执行一个简单的联接查询:
SELECT ornek_ust1.isim AS ust, ornek1_alt.ism AS alt
FROM ornek_ust1
JOIN ornek1_alt ON ornek_ust1.id = ornek1_alt.ust_ID
ORDER BY ornek_ust1.isim, ornek1_alt.ism
and within PHP, do something like: 在PHP中,执行以下操作:
$data = array();
$sql = "SELECT ...";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
$data[$row['ust'][] = $row['alt'];
}
which will build an array that looks like your desired data: 这将构建一个看起来像您想要的数据的数组:
$data = array(
'ust data1' => array(
0 => 'alt data 1'
1 => 'alt data 2'
),
'ust_data2' => ... etc...
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.