dornc9470 2011-06-29 16:35
浏览 45
已采纳

SQL结果作为PHP数组

How do I get a SQL result that will contain multiple rows into an array:

Example SQL Table:

ID      Cat        LineID      Qty    DealID    Cost
1       Phone      1           2      8941      25.00
2       Phone      2           43     8941      85.00
3       Net        1           2      8941       1.00
4       App        1           1      8941      87.00
5       Phone      1           20     8942      98.00

Would like to be able to return the result like:

$product[Phone][LineID][Qty]
$product[Phone][1][Qty] -- this would return 2

BTW: Within a Cat the LineID would never be duplicated, but it may not alway be the same LineID's - for Example under DealID 8941 there might be LineID for Cat>Phones 1,2,3,4 but under a different DealID there might only be Cat>Phones 4,5

Not sure if I'm barking up the complete wrong tree, basicly I need to loop through all the results under a DealID then cross reference the LineID to get all the information about that LineID from a another table which holds the name, image etc...Then put into a html table along the lines of:

Product Name     Qty     Cost     Line Total
Phone 1           1      85.00    85.00

I hope I have made this clear, if not don't be too harsh!!

Thanks, B.

  • 写回答

4条回答 默认 最新

  • douzi2785 2011-06-30 07:27
    关注

    This is the answer:

    $sql="SELECT * FROM Blar";
    $result = mysql_query($sql);
    $combinedResults = array();
    while ($row = mysql_fetch_array($result)) {
        $combinedResults[$row['cat']][] = array( 
        'LineID' => $row['lineID'],
        'Qty' => $row['qty'],
        'SetCost' => $row['Setcost'],
        'MonCost' => $row['Moncost'],
        'Postcode' => $row['postcode']
        );
    };
    //print_r($combinedResults);
    print_r($combinedResults['Apps'][0]['Qty']);
    

    Thanks for your help @Dereleases

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?