We can retrieve limited rows from the mysql table using the LIMIT clause. I can be used in pagination where are forced to show only limited records like 10, 20, 30, 40 etc.
You can use syntax of LIMIT into PHP function mysql_query().This function is used to execute SQL command and mysql_fetch_assoc() can be used to fetch all the selected data. This function returns row as an associative array, this function is faster then the mysql_fetch_array() function.
Let’s see the Example for mysql LIMIT clause:
<?php $hostname = 'localhost'; $username = 'root'; $pass = ''; $conn = mysql_connect($hostname, $username, $pass) or die(mysql_error()); mysql_select_db('temp_db') or die(mysql_error()); $sql = mysql_query(“SELECT id, name, age, salary FROM employee LIMIT 0, 2”); ?> <table> <tr> <th>Id</th> <th>Name</th> <th>Age</th> <th>Salary</th> </tr> <?php while($result = mysql_fetch_assoc($sql)) { ?> <tr> <td><?php echo $result[‘id’];?></td> <td><?php echo $result[‘name’];?></td> <td><?php echo $result[‘age’];?></td> <td><?php echo $result[‘salary’];?></td> </tr> <?php } ?> <table>
Output will be look like that:
Id | Name | Age | Salary |
---|---|---|---|
2 | Rahul Choudhary | 22 | 25000 |
9 | Roshan | 26 | 23000 |
4 | Nitesh Kumar | 29 | 20000 |
My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.