Hi, im working on some project on php with mysql, and I'm only actually a beginner in programming with php and mysql.

I created a table in mysql which contains several records. I have like 10 records in it. Now what I want to do is I only want to display 4 records from that table.
Here is the code on the query part. I stored it in an associative array.

<?php
	$query_latestJobPost = "SELECT * FROM tbljobad ORDER BY postDate DESC";
	$latestJobPost = mysql_query($query_latestJobPost, $dbConnection) or die (mysql_error());
	$assoc_latestJobPost = mysql_fetch_assoc($latestJobPost);
	$numberRows_latestJobPost = mysql_num_rows($latestJobPost);
?>

What happens if I use this code below, it displays the whole record which is stored in the table, but what I want is only to display a limited number of records.

<table>
			<?php do { ?>
			<tr>
				<td><?php echo $assoc_latestJobPost['jobTitle']; ?></td>
				<td><?php echo $assoc_latestJobPost['jobCategoryID']; ?></td>
				<td><?php echo $assoc_latestJobPost['postDate']; ?></td>
				<td><?php echo $assoc_latestJobPost['employerID']; ?></td>
			</tr>
			<?php }while($assoc_latestJobPost = mysql_fetch_assoc($latestJobPost)); ?> 
</table>

What will I do to arrive with that output?

try this:

<table>
			<?php while($assoc_latestJobPost = mysql_fetch_assoc($latestJobPost)) { ?>
			<tr>
				<td><?php echo $assoc_latestJobPost['jobTitle']; ?></td>
				<td><?php echo $assoc_latestJobPost['jobCategoryID']; ?></td>
				<td><?php echo $assoc_latestJobPost['postDate']; ?></td>
				<td><?php echo $assoc_latestJobPost['employerID']; ?></td>
			</tr>
			<?php } ?> 
</table>

where $latestJobPost being assigned by the mysql_query() function.

try this:

<table>
			<?php while($assoc_latestJobPost = mysql_fetch_assoc($latestJobPost)) { ?>
			<tr>
				<td><?php echo $assoc_latestJobPost['jobTitle']; ?></td>
				<td><?php echo $assoc_latestJobPost['jobCategoryID']; ?></td>
				<td><?php echo $assoc_latestJobPost['postDate']; ?></td>
				<td><?php echo $assoc_latestJobPost['employerID']; ?></td>
			</tr>
			<?php } ?> 
</table>

where $latestJobPost being assigned by the mysql_query() function.

cwarn23, thanks for the reply, but still with the code that you have revised it still arrives with the same output, still the whole record is being displayed. But, I only want limited records to be displayed, like 4 records only.

To limit it to 4 records then in addition to what I said before do the following sql query as well.

$query_latestJobPost = "SELECT * FROM tbljobad ORDER BY postDate DESC LIMIT 4";
Member Avatar for rajarajan2017
[B]$query_latestJobPost = "SELECT TOP 5 * FROM tbljobad ORDER BY postDate DESC";[/B]

Thanks sir.. I didn't know it's on the sql query.. Not yet familiar with the LIMIT keyword.. anyway.. thanks a lot.. that was a lot of help..

What's with the

TOP

keyword in the SQL statement?

Member Avatar for rajarajan2017

It will retrieve Top 5 records from the query result, you can give Top 4 to retrieve top 4 records from the query result.

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.