Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6046683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:16:05+00:00 2026-05-23T07:16:05+00:00

I have a while loop getting info from a MySQL db, outputting info via

  • 0

I have a while loop getting info from a MySQL db, outputting info via php. My code is like this:

while($row = mysql_fetch_array($result)) {
  $output .= '<tr>' . "\r";
  $output .= '<td>' . $row['lnID'] . '</td>' . "\r";
  $output .= '<td>' . $row['lnLine'] . '</td>' . "\r";
  $output .= '<td>' . $row['lnTitle'] . '</td>' . "\r";
  $output .= '<td>' . $row['cLongName'] . '</td>' . "\r";
  $output .= '</tr>' . "\r";
}

The cLongName can have multiple entries in the database, so it’s outputting data in this form:

lnID  | lnLine  | lnTitle  | cLongName
---------------------------------------
lnID1 | lnLine1 | lnTitle1 | cLongname1
lnID1 | lnLine1 | lnTitle1 | cLongname2
lnID1 | lnLine1 | lnTitle1 | cLongname3
lnID2 | lnLine2 | lnTitle2 | cLongname1
lnID2 | lnLine2 | lnTitle2 | cLongname2
lnID2 | lnLine2 | lnTitle2 | cLongname3
lnID2 | lnLine2 | lnTitle2 | cLongname4
lnID2 | lnLine2 | lnTitle2 | cLongname5

I’d like to have the data in this format:

lnID  | lnLine  | lnTitle  | cLongName
---------------------------------------------------------------------------------------
lnID1 | lnLine1 | lnTitle1 | cLongname1, cLongname2, cLongname3
lnID2 | lnLine2 | lnTitle2 | cLongname1, cLongname2, cLongname3, cLongname4, cLongname5

I’ve tried bunging in a foreach loop in my while loop but have got nowhere with it. Any ideas or help would be appreciated.

— EDIT —

My MySQL query is as follows:

$result = mysql_query("
  SELECT
    pcdb_line.lnID, pcdb_line.lnLine, pcdb_line.lnTitle,
    pcdb_colourways.cwLine, pcdb_colourways.cwColID1,
    pcdb_colour_name.cID, pcdb_colour_name.cLongName, pcdb_colour_name.cHex
  FROM pcdb_line
  GROUP BY pcdb_line.lnID
  LEFT JOIN pcdb_colourways
  ON (pcdb_line.lnID = pcdb_colourways.cwLine)
  LEFT JOIN pcdb_colour_name
  ON (pcdb_colour_name.cID = pcdb_colourways.cwColID1)
  LIMIT 50
");

As per ADW’s answer, I tried group_concat on just the cLongName, on all pcdb_colour_name‘s but it’s not giving me any output.

— EDIT 2 —

Changing the SELECT query to this:

  $result = mysql_query("
    SELECT
      pcdb_line.lnID, pcdb_line.lnLine, pcdb_line.lnTitle,
      pcdb_colourways.cwLine, pcdb_colourways.cwColID1,
      pcdb_colour_name.cID, group_concat(pcdb_colour_name.cLongName) AS cLongNames, pcdb_colour_name.cHex
    FROM pcdb_line
    LEFT JOIN pcdb_colourways
    ON (pcdb_line.lnID = pcdb_colourways.cwLine)
    LEFT JOIN pcdb_colour_name
    ON (pcdb_colour_name.cID = pcdb_colourways.cwColID1)
    GROUP BY pcdb_line.lnID
    LIMIT 50
  ");

Allowed me to use $output .= "\t\t" . '<td>' . $row['cLongNames'] . '</td>' . "\r"; on my php, giving me the required output.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:16:05+00:00Added an answer on May 23, 2026 at 7:16 am

    You’d probably be better modifying your database query to something like:

    select lnID,lnLine,lnTitle,group_concat(cLongName) from table group by lnID,lnLine,lnTitle;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code below that is getting called from a while loop so it's
I have got a while loop getting usernames from a SQL database. Thanks
I have a while loop issue in PHP that seems like it could have
The while loop I have while reading in from a file doesn't break. I'm
I have a problem with my php/mysql script. It should only output the while
I made a while loop in php: <?php include_once(connect.php); $search=mysql_query(SELECT * from chat ORDER
I keep getting my code caught in an infinite while loop. It is nothing
I have a while loop in php that fetches assoc result of a query
I have a script that uses a simple while loop to display a progress
I have a tabled view in a while loop, where a user can view

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.