PHP Formatting output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gsashwin
Forum Newbie
Posts: 4
Joined: Thu Nov 18, 2010 12:11 am

PHP Formatting output

Post by gsashwin »

Hi, I am trying to print my database records into a php page. I am able to succesfully print the values however the values are being duplicated. For example If I have two records in the employee_details, every these two records are being printed twice on my php page. can anyone tell me the reason why? The output that I see is as follows

7 7 3a7cf5162a9c0a5014c92021e7ca0bf0 3a7cf5162a9c0a5014c92021e7ca0bf0 Bryan Bryan 4111 4111 Admin Admin
6 6 6743c3d1519ab4f2cd9a78ab09a511bd 6743c3d1519ab4f2cd9a78ab09a511bd Raul Raul 601 W Yandell Dr, 601 W Yandell Dr, Admin Admin


<html>
<head>
</head>
<body>
<?php
mysql_connect("localhost","root","");
mysql_select_db("encryption") or die(mysql_error());
$query = mysql_query("select * from employee_details");
?>

<table>
<?php

for($counter = 0;$row=mysql_fetch_array($query); $counter++)
{
print ("<tr>");
foreach($row as $key=> $value)
print ("<td>$value</td>");
print ("</tr>");
}
?>
</table>



</body>
</html>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Formatting output

Post by requinix »

Read the documentation for mysql_fetch_array very carefully. Especially the Return Values section.
User avatar
icepick
Forum Newbie
Posts: 5
Joined: Fri Mar 13, 2009 9:37 am
Location: Portugal

Re: PHP Formatting output

Post by icepick »

Do it this way

Code: Select all


<?php
mysql_connect("localhost","root","");
mysql_select_db("encryption") or die(mysql_error());
$query = mysql_query("select * from employee_details");

$table.="<table>";


while($row=mysql_fetch_row($query) ) {
  $table.="<tr>";
  $counter=0;
  while ($row[$counter]) { 
      $table.="<td>$row[$counter]</td>";
      $counter++;
      }
  $table.="</tr>";
  }

$table.="</table>";

echo $table;

?>

User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Formatting output

Post by requinix »

Oh, well, if we're just going to tell him what to do instead of having him learn it himself,

Code: Select all

while ($row = mysql_fetch_row($query)) {
	$table .= "<tr>";
	foreach ($row as $cell) $table .= "<td>{$cell}</td>";
	$table .= "</tr>";
}
Post Reply