Marcos01 Posted September 16, 2008 Share Posted September 16, 2008 Hi all, I would like to display everything from a table in the browser. Do i need to use fetch_array or fetch_assoc? And what loop would work best? Thanks. The code i have so far: $sql = "SELECT * FROM `trainingsdata` WHERE 1 LIMIT 0, 30 "; $result = mysql_query($sql) or die('Eror selecting data'); $row = mysql_fetch_array($result); $num = mysql_num_rows($result); Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/ Share on other sites More sharing options...
Marcos01 Posted September 16, 2008 Author Share Posted September 16, 2008 I added a loop but get Resource id #5 multiple times Here the code so far: $sql = "SELECT * FROM `trainingsdata` WHERE 1 LIMIT 0, 30 "; $result = mysql_query($sql) or die('Error selecting data'); $row = mysql_fetch_array($result); $num = mysql_num_rows($result); for ($i=0; $i<$num; $i++) { echo $result; } Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642729 Share on other sites More sharing options...
PFMaBiSmAd Posted September 16, 2008 Share Posted September 16, 2008 Directly out of the mysql_fetch_array section of the php manual - $result = mysql_query("SELECT id, name FROM mytable"); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { // code to access your fields in the $row array } Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642738 Share on other sites More sharing options...
Marcos01 Posted September 16, 2008 Author Share Posted September 16, 2008 Thank you PFMaBiSmAd, But can you explain on how to use this? Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642753 Share on other sites More sharing options...
JasonLewis Posted September 16, 2008 Share Posted September 16, 2008 $row becomes an array, and the keys are the name of the columns in your table. Inside the loop put this: echo $row['columnName']; echo "<br />"; Where columnName is the name of a column in the table you are selecting data from. Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642755 Share on other sites More sharing options...
Marcos01 Posted September 16, 2008 Author Share Posted September 16, 2008 thanks Thanks ProjectFear, I still get Resource id #5 multiple times This is the code i have now: $result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata "); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo $row['columnName']; echo "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642769 Share on other sites More sharing options...
PFMaBiSmAd Posted September 16, 2008 Share Posted September 16, 2008 So, what is the actual data in your table? Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642771 Share on other sites More sharing options...
Marcos01 Posted September 16, 2008 Author Share Posted September 16, 2008 I have a date, course and place. And want to display them all in the browser. Now I get resource Resource id #5 as many times as I have rows in the database. Thanks for helping me. Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642773 Share on other sites More sharing options...
PFMaBiSmAd Posted September 16, 2008 Share Posted September 16, 2008 With the code you just posted, that would mean your data in your table is not what you think it is. Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642777 Share on other sites More sharing options...
JasonLewis Posted September 16, 2008 Share Posted September 16, 2008 Hmm, try this: $result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") or die(mysql_error()); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<pre>", print_r($row), "</pre><br />"; } And see if anything is outputted then. Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642781 Share on other sites More sharing options...
Marcos01 Posted September 16, 2008 Author Share Posted September 16, 2008 I am getting this as output: Array ( [id] => 1 [datum] => 2008-09-09 [cursus] => php [plaats] => Washington ) 1 Array ( [id] => 2 [datum] => 2008-09-09 [cursus] => php [plaats] => New York ) 1 Array ( [id] => 3 [datum] => 2008-09-09 [cursus] => php [plaats] => Chicago ) etc etc. The code so far: $result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") or die(mysql_error()); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<pre>", print_r($row), "</pre><br />"; } I would like to have the output on one row. Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642793 Share on other sites More sharing options...
JasonLewis Posted September 16, 2008 Share Posted September 16, 2008 Strange, before it should've been echoing something. Try this now: $result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") or die(mysql_error()); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo $row['cursus']; echo "<br />"; } See if that outputs anything. Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642801 Share on other sites More sharing options...
PFMaBiSmAd Posted September 16, 2008 Share Posted September 16, 2008 Strange, before it should've been echoing something. The posted code was likely not the actual code. Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642807 Share on other sites More sharing options...
Marcos01 Posted September 16, 2008 Author Share Posted September 16, 2008 Thanks ProjectFear, It now displays all from cursus. So when I add date and place it works. But doesn't show nicely. Here is the code now: $result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") or die(mysql_error()); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo $row['datum']." ".$row['cursus']." ".$row['plaats']; echo "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642813 Share on other sites More sharing options...
JasonLewis Posted September 16, 2008 Share Posted September 16, 2008 Well you need to format it then. You can use normal markup here, just your basic HTML. Just make sure you place it in the right spots. For example: $result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") or die(mysql_error()); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "Date: ".$row['datum']."<br />Cursus: ".$row['cursus']."<br />Plaats: ".$row['plaats']; echo "<br /><br />"; } Of course you can do anything else, like place it in a table. Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642814 Share on other sites More sharing options...
Marcos01 Posted September 16, 2008 Author Share Posted September 16, 2008 PFMaBiSmAd and ProjectFear, I made an error saving the changes to an older file. Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642816 Share on other sites More sharing options...
Marcos01 Posted September 16, 2008 Author Share Posted September 16, 2008 ProjectFear and PFMaBiSmAd, Thats what I need. Thank you both for your help! Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642819 Share on other sites More sharing options...
Adam Posted September 16, 2008 Share Posted September 16, 2008 $result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata "); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo $row['columnName']; echo "<br />"; } Perhaps the problem... echo $row['columnName']; ...did you replace "columnName" with an actual field name from your table? Adam Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642826 Share on other sites More sharing options...
Marcos01 Posted September 16, 2008 Author Share Posted September 16, 2008 Hi Adam, yes I did. And works well now. This is the final code: $result = mysql_query("SELECT id, datum, cursus, plaats FROM trainingsdata ") or die(mysql_error()); echo '<table cellpadding="5" cellspacing="5"><tr>'; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<td>".$row['datum']."</td><td>".$row['cursus']."</td><td> ".$row['plaats']."</td><tr>"; } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/124464-solved-display-table-from-database-in-browser/#findComment-642829 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.