Jump to content

Show MySQL Rows


neex1233

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/164882-show-mysql-rows/
Share on other sites

<?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 :)

Link to comment
https://forums.phpfreaks.com/topic/164882-show-mysql-rows/#findComment-869483
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.