編輯:: 也許我應該問什麼從數據庫中得到結果集的正確方法是。當你有5個連接有1:M的關係時,你是否需要5次不同的時間才能訪問數據庫?陣列幫助。尋找更好的方法
我在一個小時前問過這個問題,但一直沒有找到合適的答案。我繼續寫了一些代碼,但它正是我所需要的,但我正在尋找更好的方法來做到這一點
這個數組給我多行,其中只有一些需要一次而其他需要多次。我需要按照以下所述過濾這些內容,但如果可能的話,希望有更好的方法來做到這一點。
Array
(
[0] => Array
(
[cid] => one line
[model] => one line
[mfgr] => one line
[color] => one line
[orderid] => one line
[product] => many lines
[location] => many lines
)
[1] => Array
(
.. repeats for as many rows as were found
)
)
這段代碼完美地工作,但我認爲有一個更有效的方法來做到這一點。有沒有PHP函數可以讓我清理一下?
// these are the two columns that produce more than 1 result.
$product = '';
$orderid = '';
foreach($res as $key)
{
// these produce many results but I only need one.
$cid = $key['cid'];
$model = $key['model'];
$mfgr = $key['mfgr'];
$color = $key['color'];
$orderid = $key['orderid'];
// these are the two columns that produce more than 1 result.
if($key['flag'] == 'product')
{
$product .= $key['content'];
}
if($key['flag'] == 'orderid')
{
$orderid .= $key['content'];
}
}
// my variables from above in string format:
這裏是請求的SQL
SELECT
cid,
model,
mfgr,
color,
orderid,
product,
flag
FROM products Inner Join bluas ON products.cid = bluas.cid
WHERE bluas.cid = 332
ORDER BY bluas.location ASC
使用數據庫? – knittl 2010-03-02 10:57:48
是的,結果來自數據庫。 – jim 2010-03-02 10:58:45
使用where語句過濾數據庫... – knittl 2010-03-02 11:09:04