gsashwin Posted November 28, 2010 Share Posted November 28, 2010 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> Link to comment https://forums.phpfreaks.com/topic/220052-formatting-output/ Share on other sites More sharing options...
satya61229 Posted November 28, 2010 Share Posted November 28, 2010 Here for($counter = 0;$row=mysql_fetch_array($query); $counter++) I think you are starting from 0 and going till last +1 . Try with usual while($row=mysql_fetch_assoc($query) {} Link to comment https://forums.phpfreaks.com/topic/220052-formatting-output/#findComment-1140550 Share on other sites More sharing options...
Pikachu2000 Posted November 28, 2010 Share Posted November 28, 2010 You're using mysql_fetch_array(), which returns not one, but two arrays of the same data; one associative array, and one enumerated array. When you loop through with foreach, you get the output of both. Change mysql_fetch_array() to mysql_fetch_row() or mysql_fetch_assoc() and your problem should be fixed. Link to comment https://forums.phpfreaks.com/topic/220052-formatting-output/#findComment-1140590 Share on other sites More sharing options...
menator Posted November 28, 2010 Share Posted November 28, 2010 Is there any reason that you are using two loops instaed of one? I don't know the fields of your table but, my example is: $query = mysql_query("select * from employee_details"); while($row = mysql_fetch_array($query)) { echo '<td>' . $row['field'] . '</td>'; } Link to comment https://forums.phpfreaks.com/topic/220052-formatting-output/#findComment-1140687 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.