Limiting number of rows in a query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
rocknrisk
Forum Newbie
Posts: 16
Joined: Tue Mar 14, 2006 8:09 pm

Limiting number of rows in a query

Post by rocknrisk »

HI all,

This is what I have so far, and it works...

Code: Select all

class clsNextShows { // List the next 3 shows for the right-hand menu

	function fctNext3Shows () {
	
		require('Connections/damsel.php');
		mysql_select_db($database_damsel, $damsel);
		
		$query_rstNextShows = "SELECT * FROM mtblshows WHERE dteStartDate > CURRENT_DATE ORDER BY dteStartDate ASC";
		$rstNextShows = mysql_query($query_rstNextShows, $damsel) or die(mysql_error());
		
		$totalRows_rstNextShows = mysql_num_rows($rstNextShows);
		
		while ($arrNextShows = mysql_fetch_array($rstNextShows)) {
			$txtSN = $arrNextShows ['txtShowName'];
			$lnkLink = $arrNextShows ['txtWebsite'];
			
			echo '<a href="'.$lnkLink.'">'.$txtSN.'</a><br>';
		}
		
		mysql_free_result($rstNextShows);
	}
} // End clsNextShows
What I need to do is only List the top 3... How do I do this, please???

I have tried creating a counter in the while statement but that didn't work (or I did not do it correctly)...

Something like:

Code: Select all

$counter = 1;
		while (($arrNextShows = mysql_fetch_array($rstNextShows))  && ($counter <= 3)) {
			$txtSN = $arrNextShows ['txtShowName'];
			$lnkLink = $arrNextShows ['txtWebsite'];
			
			echo '<a href="'.$lnkLink.'">'.$txtSN.'</a><br>';
                                                $counter++;
		}
Thank you in advance for any help...

Clinton
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Limiting number of rows in a query

Post by Benjamin »

Code: Select all

SELECT * FROM mtblshows WHERE dteStartDate > CURRENT_DATE ORDER BY dteStartDate ASC Limit 0,3
Post Reply