louiscb Posted October 22, 2014 Share Posted October 22, 2014 (edited) I'm a beginner to PHP and am stuck as to how I can fetch data from a MYSQL database and output that information in html to the website. Currently I get the information from the database using normal php: $result = mysql_query("SELECT * FROM truffleProducts"); Then I assign each field from the database to a php variable in a while loop: while ($row = mysql_fetch_array($result)) { $id = $row{'id'}; $name = $row{'name'}; $price = $row{'Price_per_kg'}; $season = $row{'Season'}; $country = $row{'Country'}; $image = $row{'image'}; $review = $row{'review'}; } Then I just print each variable in a table using simple html and php. My problem is that it only outputs the latest element or row from my database. I want to output the database within a HTML template so that it is professional and tidy, but i cant figure out how to do that. Any help? Edited October 22, 2014 by louiscb Quote Link to comment https://forums.phpfreaks.com/topic/291980-help-outputting-data-from-mysql-database-using-php-for-a-website/ Share on other sites More sharing options...
cyberRobot Posted October 22, 2014 Share Posted October 22, 2014 With each iteration of the loop, the variables will be overwritten. To display all the results, you could output the information within the while loop. Perhaps the following example will help: http://www.tizag.com/mysqlTutorial/mysqlfetcharray.php Quote Link to comment https://forums.phpfreaks.com/topic/291980-help-outputting-data-from-mysql-database-using-php-for-a-website/#findComment-1494378 Share on other sites More sharing options...
Barand Posted October 22, 2014 Share Posted October 22, 2014 I find the easiest way is <table> <?php while ($row = mysql_fetch_row($result)) { echo "<tr><td>" . join('</td><td>',$row) . "</td></tr>\n"; } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/291980-help-outputting-data-from-mysql-database-using-php-for-a-website/#findComment-1494381 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.