Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.