Page 1 of 1

Display each field in mysql table in separate rows

Posted: Sat Mar 07, 2009 7:45 pm
by harleyridin
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 (bcrattend) with the table (attendees) and fields: name, email, age, date. I use the following to retrieve and display the data:

Code: Select all

 
<?php
mysql_connect("localhost","name","password");
//if (!$con)
//  {
//  die('Could not connect: ' . mysql_error());
//  }
 
mysql_select_db("bigcross_bcrattend");
 
$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

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

harleyridin

Code: Select all

 

Re: Display each field in mysql table in separate rows

Posted: Sat Mar 07, 2009 8:02 pm
by Benjamin
Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]