我有一個表與各種拍賣,其中每個記錄都有用戶名和類別。很可能,會有多個具有相同用戶名和類別的記錄。有四種不同的類別。動態命名變量?
我想知道是否有可能,在下面的準備查詢,讓第二個綁定參數是循環包含4個類別的數組的結果,如果我可以動態地將結果分配給數組?
$countAuctionsQuery = "select COUNT(USERNAME, SUBCAT) from AUCTIONS where username = ? AND SUBCAT = ?";
if ($getRecords = $con->prepare($countAuctionsQuery))
{
$getRecords->bind_param("ss", $username, $subcat);
$getRecords->execute();
$getRecords->bind_result($numRecords);
}
編輯:
數據
Auctions
username itemnumber cost category
------------------------------------------------
fredx 222 $33 fake
fredx 123 $43 fake
timo 765 $54 fake
fredx 987 $99 sold
bobk 233 $77 fake
wenx 11 $12 ok
fredx 23 $31 ok
fredx 723 $73 fake
wenx 44 $88 ok
所以,對用戶名和fredx類假,3應退還的一個例子。
對於用戶名fredx和類別出售,1應返回
對於用戶名和蒂莫類假,1應返回
對於用戶名和wenx類別確定; 2應返回。
我希望能夠打印出像這樣:在一個循環中
SELECT subcat, COUNT(*)
FROM AUCTIONS
WHERE username = ?
GROUP BY
subcat
,並獲取結果:
$countAuctionsQuery = $query;
if ($getRecords = $con->prepare($countAuctionsQuery)) {
$getRecords->bind_param("s", $username);
$getRecords->execute();
$getRecords->bind_result($subcat, $numRecords);
while ($getRecords->fetch()) {
print "$subcat items: $numRecords items of category['$subcat']";
}
}
我需要能夠打印出像 假項目:$ numfakeitems 銷售的物品:$ numsolditems oktiems:$ numokitems等,這就是爲什麼我認爲它是有道理的使用數組。目前我不認爲我可以只用一個變量來做到這一點,除非我誤解了。 – 2009-04-20 14:20:06