简体   繁体   中英

Mysql and PHP select and display columns

There's the problem. I need to display the fields of PJ1 to PJ8 but just the same row. Each row is a different account. So, how could I take the content of the fields and display them like this:

  • Account one: PJ1
  • Account one: PJ2
  • Account one: PJ3...

and NOT like this:

  • Account one: PJ1
  • Account two: PJ1
  • Account three: PJ1...

Here's the structure of the table.

在此处输入图片说明

Thank you very much.

You need to iterate through each field in the row/record, rather than just iterating through each row in the result.

So you have something along the lines of:

while ($row = mysql_fetch_row($query_result)) {
    echo $row[0];
}

But what you really want is:

while ($row = mysql_fetch_row($query_result)) {
    foreach ($row as $field) {
        echo $field;
    }
}

Use UNION

SELECT * FROM (
    SELECT account_num, PJ1
    FROM table
    UNION
    SELECT account_num, PJ2
    FROM table
    UNION
    SELECT account_num, PJ3
    FROM table
    ...
)
ORDER BY account_num
$query="SELECT * FROM table ";
$result=mysql_query($query);
$i=0;
while($row=mysql_fetch_assoc($result)
{
 echo "account no :-"."$i"."$row['pj1']"."</br>";//you can add more column
 $i++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM