Jump to content

Display each field in mysql table in separate rows


harleyridin

Recommended Posts

I have searched exhaustively and not found what I need. Here is my situation.

 

I need to display mysql data with each field on a separate row. I am currently using a mysql database  (version 5.0.75-community-log) with the table (attendees) and fields: name, email, age, date. I use the following to retrieve and display the data:

...

$result = mysql_query("SELECT * FROM attendees order by date");

echo "<table border='1'>
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
<th>Date</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "<td>" . $row['age'] . "</td>";
  echo "<td>" . $row['date'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
//mysql_close($con);
?>

 

This displays all the records in columns like this:

 

Name Email  Age  Date

data  data  data  data

data  data  data  data

 

I am going to change my query so I only display one record and I need to have it displayed as:

 

Name: data

Email: data

Age: data

Date: data

...

$result = mysql_query("SELECT * FROM attendees order by date");

echo "<table border='1'>
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
<th>Date</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
echo "<table border='1'>
<tr><th>Name</th><td>".$row['name']."</td></tr>
<tr><th>Email</th><td>".$row['email']."</td></tr>
<tr><th>Age</th><td>".$row['age']."</td></tr>
<tr><th>Date</th><td>".$row['date."</td></tr>
</table>";

  }
//mysql_close($con);
?>

It will work if you only select one piece of data.

I really appreciate your response.  I changed my query to select one record and put the new code in.  I got the following error message:

 

Warning: Unexpected character in input: ''' (ASCII=39) state=1 in /home/bigcross/public_html/dbcheck.php on line 30

 

Parse error: syntax error, unexpected ';', expecting ']' in /home/bigcross/public_html/dbcheck.php on line 31

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.