Monday, July 2, 2012

Mysql Limit

Mysql Limit

Previous post we were discussed pagination to limit the number of records display in mysql table.By adding limit to the query also we can limit the number of row display in a table.





First Name Last Name
Scarlett Johansson
Clooney Anderson
Kate Hudson
Scarlett Clooney
Kate Eva


This example too,I have taken same table called "namelist" in my_database

<?php
$dbHost = 'localhost';
$dbUser = 'root';
$dbPass = '';
$dbName = 'my_database';
$dbConn = mysql_connect ($dbHost,$dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($dbName) or die('Cannot select database. ' . mysql_error());
?>
<html>
<head>
<title>Display</title>
</head>
<center><form name="form" action="" method="post">
<select name="limit" onchange="this.form.submit()">
<option readonly="readonly">Number</option>
<option>5</option>
<option>10</option>
<option>15</option>
<option>20</option>
</select>
</form></center>
<table align="center" bgcolor=#ffffff>
<tr bgcolor=#f2e81d>
<th> First Name</th>
<th>Last Name </th>
</tr>
<?php
$limit=$_POST['limit'];
$query = "SELECT * FROM namelist LIMIT 0, $limit";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)){
?>
<tr bgcolor=#f0eda4><td><?php echo $row["FirstName"];?></td>
<td><?php echo $row["LastName"];?></td>
</tr>
<?php }?>
</table>
</body>
</html>

No comments:

Post a Comment