打印下面的单个字段

时间:2010-10-24 16:25:14

标签: php mysql

假设我做了这样的选择:

select company, location, acnum, bills from table

我如何使用php输出数据,以便公司和位置保持在最顶层,并打印每个账单,并在底部显示acnum。我正在使用mysql_fetch_assoc并且疯了。我可以记录每一条记录,但不能在顶部和底部得到一条。

我正试图像这样获取数据:

 Company name here Location here
 Bill
 Bill
 Bill
 Bill

 Acnum goes here.

P.S我是PHP的新手。我曾经在ASP中编程。

1 个答案:

答案 0 :(得分:0)

如果你有公司ID,你可以这样做:

    $sql = 'SELECT company, location, acnum 
    FROM Companies
    WHERE id = '.$id

    $result = mysql_query($sql);

    $company = mysql_fetch_assoc($result)) {
    mysql_free_result($result);   

    $sql = 'SELECT bills 
    FROM Companies
    WHERE id = '.$id

    $result = mysql_query($sql);

    while ($row = mysql_fetch_assoc($result)) {
        $bills[] = $row;
    }

    mysql_free_result($result);

    echo $company['company'] . $company['location'];

    foreach($bills as $bill) {
      echo $bill['bills'];
    }

    echo $company['acnum'];