0

I have a query to output data within my MYSQL database and would like it to display in a table. With "Name" and "UserID" as the headings... currently the code I have developed, does output the results but instead creates multiple instances of the table and doesn't create new rows.

Please could someone help me achieve my goal?

echo"
<table width='400' border='1'>               
<tr>
    <td>Name</td>
    <td>SystemID</td>
  </tr>
  <tr>
    <td> ".$row['FullName']." </td>
    <td> ".$row['UserID']." </td>
  </tr>
</table>";
2
  • 2
    did you googled before asking this question.? Commented Aug 30, 2012 at 14:43
  • Yes, but I know how to output tables, I just couldn't find why multiple instances were being created, until it was pointed out that I had it in my loop Commented Aug 30, 2012 at 14:54

7 Answers 7

2

The Table should be defined outside of the loop of the data, you just loop through the data and add a row on every loop :

echo "<table width='400' border='1'> ";
echo "<tr>
    <td>Name</td>
    <td>SystemID</td>
  </tr>";

// Example of while loop :
   while ($row = mysql_fetch_array($results)) {
     echo"      <tr>
        <td> ".$row['FullName']." </td>
        <td> ".$row['UserID']." </td>
      </tr>";
   }

echo "</table>";
Sign up to request clarification or add additional context in comments.

Comments

2

If the code you have shown is within some kind of while loop that loops through the database results, it should be clearly evident why you get a bunch of one row tables. Your entire table output would happen with each loop execution. What you should be doing is outputting your table opening tag and header row before you start the data retrieval loop. Output only the row with each loop execution, and then output the table closing tag after you exit the loop.

Comments

2
echo"
<table width='400' border='1'>               
<tr>
    <td>Name</td>
    <td>SystemID</td>
  </tr>";

while ($row = $result->fetch_assoc()) { // or whatever your retrieval code is
  echo "<tr>
    <td> ".$row['FullName']." </td>
    <td> ".$row['UserID']." </td>
  </tr>";
}

echo "</table>";

You need to echo the table open tag and headers then loop through the database results, then echo the table closing tag.

1 Comment

I see where I went wrong now, makes sense, a rookie mistake :)
2

You should put <table width='400' border='1'>
<tr> <td>Name</td> <td>SystemID</td> </tr>
before loop ( <HERE> while() { ... } AND </table> you should put after cycle...

it should look like this:

echo "
<table width='400' border='1'>
<tr>
    <td>Name</td>
    <td>SystemID</td>
  </tr>";

while(....) {
echo "               
 <tr>
    <td> ".$row['FullName']." </td>
    <td> ".$row['UserID']." </td>
  </tr>
";
}

echo "</table>";

Comments

1

http://www.siteground.com/tutorials/php-mysql/display_table_data.htm

Comments

0

Use this inside the loop instead

echo"
  <tr>
    <td> ".$row['FullName']." </td>
    <td> ".$row['UserID']." </td>
  </tr>";

and put the rest of the table wrapping around the loop.

Comments

0

Don't complicate the mission ; you don't need to do loop ; mysql provides option whichi is --html , just use it :

mysql --html -uroot -e "USE mydb;select * from mytable"

if you want to display only : FullName and UserID :

mysql --html -uroot -e "USE mydb;select FullName,UserID from mytable"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.