neex1233 Posted July 6, 2009 Share Posted July 6, 2009 Hi! I made this show news script, but I have just one problem! This script only shows the first entry in the MySQL database. <?php $con = mysql_connect("localhost", "Username", "Password") or die('Could not connect: ' . mysql_error()); mysql_select_db("DB_Name", $con); $sql = " SELECT * FROM `news` LIMIT 0, 30 "; $result = mysql_query($sql) or die (mysql_error()); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $title = $row['title']; $poster = $row['poster']; $text = $row['text']; $time = $row['time']; $date = $row['date']; } $text = stripslashes($text); $title = stripslashes($title); $text = wordwrap($text, 15, "<br/>\n"); echo "<font size='4'><b>$title</b></font><br>"; echo "Posted by $poster "; echo "on $date at $time<br>"; echo "$text"; ?> Any help is appreciated. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/164882-show-mysql-rows/ Share on other sites More sharing options...
Philip Posted July 6, 2009 Share Posted July 6, 2009 Each time you loop through your results, you're overwriting the variable's value. You should either use an array and save the results, or print from the loop itself. Quote Link to comment https://forums.phpfreaks.com/topic/164882-show-mysql-rows/#findComment-869476 Share on other sites More sharing options...
neex1233 Posted July 6, 2009 Author Share Posted July 6, 2009 So how would I do that? Quote Link to comment https://forums.phpfreaks.com/topic/164882-show-mysql-rows/#findComment-869479 Share on other sites More sharing options...
Philip Posted July 6, 2009 Share Posted July 6, 2009 <?php $con = mysql_connect("localhost", "Username", "Password") or die('Could not connect: ' . mysql_error()); mysql_select_db("DB_Name", $con); $sql = " SELECT title, poster, text, time, date FROM `news` LIMIT 0, 30 "; $result = mysql_query($sql) or die (mysql_error()); $results = array(); // initialize the array while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { // create a new element in the results array, and set the mysql row to it $results[] = $row; } // now we need to loop through each of the array elements. foreach($results as $row) { // you can call the value like $row['name'] $text = stripslashes($row['text']); $title = stripslashes($row['title']); $text = wordwrap($text, 15, "<br/>\n"); echo "<font size='4'><b>$title</b></font><br>"; echo "Posted by {$row['poster']} "; echo "on {$row['date']} at {$row['time']}<br>"; echo "$text"; } ?> See the comments in the code Quote Link to comment https://forums.phpfreaks.com/topic/164882-show-mysql-rows/#findComment-869483 Share on other sites More sharing options...
neex1233 Posted July 6, 2009 Author Share Posted July 6, 2009 Awesome, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/164882-show-mysql-rows/#findComment-869487 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.