Hello everyone, i working on a website and i wanna grab data from a table in my database . specifically the last 4 rows that are entered as the database is updated automatically by users. i wanna be able to display each row separately in different div tags. i'm getting a bit of trouble. As i think the data from the table has to be stored in an array and i'm a bit weak in that area. here's what i've done thus far. an example of my div out put is shown below the php tags

<?php
//Perform database query 
$results = mysql_query("SELECT Title FROM News ORDER BY Newsid DESC", $db);
if (!$results) {
	die("Database query failed: " . mysql_error());
		}
 while($row = mysql_fetch_array($results))
{   
   if($row[6] == 0)
	{
	   if(strlen($row[4]) > 600)
		{
		  $brief = substr($row[4], 0, 600) . '...';
		} 
	else
	{
	$brief = $row[4];
	}
} 
else
{
  $brief = $row[5];
}

}					
?>



<div>
<img src="images/image01.jpg" alt="alt text" title="description "/>
<h3><?php  echo $row[?] ?></h3>
<p>	</p>
<a href="http://www.google.com">Link #1</a>
</div>

Dani AI

Generated

The root problems in the posted code are not the database engine alone but where and how the data is selected and rendered. selected only a single column but later used numeric indexes (like row[4], row[6]) and tried to print $row outside the fetch loop — that will either be wrong or only show the last fetched row. and were correct to suggest restricting the query to four rows, but there are a few more practical fixes to make the output reliable and safe.

Practical checklist to fix this reliably:

  • SELECT the exact columns you need (title, body, id, flags, image path) instead of relying on position-based indexes. Use associative fetch so you can reference fields by name and avoid off-by-index bugs.
  • Generate each <div> while fetching a row, or store each associative row in an array and then loop over that array to print. Do not build all HTML before the fetch or try to print a fetch-variable after the loop.
  • When truncating content, use a multibyte-safe routine if your site uses UTF-8; otherwise substr can break characters. Always escape text for HTML output (use htmlspecialchars equivalents) so user content cannot break the page or inject scripts.
  • If you want the newest four but presented oldest-first, fetch the four newest and then reverse the fetched array before output.

Modernization and safety notes:

  • The old mysql_* extension is deprecated; migrate to mysqli or PDO, set the connection charset to utf8mb4, and use prepared statements for any user-supplied inputs.
  • Add basic error checking: detect an empty result set, log query errors, and validate that the column you order by is actually monotonic (use a timestamp column if IDs might not be sequential).

Quick troubleshooting tips:

  • Dump one fetched row with var_dump to confirm column names and values before building HTML.
  • Validate the produced HTML (watch for malformed anchors like nested <a> tags).
  • If results look wrong, run the SQL directly in your DB client to confirm ordering and returned columns.

Recommended Answers

All 2 Replies

if you want the last 4, you should order by DESC and add LIMIT 4 into the query

Simply Change:

$results = mysql_query("SELECT Title FROM News ORDER BY Newsid DESC", $db);

To:

$results = mysql_query("SELECT Title FROM News ORDER BY Newsid DESC LIMIT 4", $db);

This will tell mysql to limit the query to the first 4 results making the query faster.

As for putting in the div tags just change wherever you get data to reflect that i.e.

$brief = '<div>'.substr($row[4], 0, 600) . '...<div>';

or

$brief = '<div>'.$row[4].'</div>';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.