SQL结果作为PHP数组 [英] SQL Results as PHP Array

查看:150
本文介绍了SQL结果作为PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何得到一个SQL结果将包含多个行到一个数组:

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

例如,SQL表:

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

顺便说一下:在一个猫的另一LineID绝不会被复制,但它可能不永久是相同的另一LineID的 - 为DealID 8941下实施例有可能被另一LineID为猫>电话1,2,3,4但在不同DealID有可能只是猫>电话4,5

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

不知道如果我吠叫完全错了,basicly我需要遍历所有下DealID然后交叉引用的另一LineID的结果让所有有关从另一个表的另一LineID持有该名称的信息,图像等...然后放入一个HTML表格线沿线的:

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!!

谢谢,
B点。

Thanks, B.

推荐答案

这是答案:

$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']);

感谢您的帮助@Dereleases

Thanks for your help @Dereleases

这篇关于SQL结果作为PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆