Skip to main content

How to fetch n latest numbers of record from a MySQL table using PHP in an ascending or descending order?

If you are new on PHP and MYSQL and wondering how to fetch just n numbers of records from a database table and group them by ascending or descending order, then you are at right place.


Lets suppose a database table as below:

Database name : “comments”
Table name : “comments”

Fields 
  • id (Auto-increment)
  • name (varchar 100)
  • comment (varchar 100)

Mysql statement to fetch 5 latest records and sort by descending order 

SELECT * FROM comments ORDER BY id DESC LIMIT 5

Full Php Code:
    <?php $hostname="localhost"; $username="root"; $password="root"; $conn=mysql_connect("$hostname","$username","$password"); mysql_select_db("comments"); //default password for mamp is "root" and for xampp is empty $find_database = mysql_query("SELECT * FROM comments ORDER BY id DESC LIMIT 5"); if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } else { while($row = mysql_fetch_assoc($find_database)) { $name=$row['name']; $comment=$row['comment']; echo'<font size="4" color="purple"> <b>'.$name.'</b>:'.$comment.'</font><hr></div>'; } } ?>
It has been assumed that your table has auto-increment field “id” . 

Comments

Popular posts from this blog

How to read nth line of a text file in C programming using file handling?

C program to read the nth line of a file.  This file handling C program illustrates how to read the nth content of a text file.  Suppose your text filename is "documents.txt" and you want to read only 9th line of that file. Here's a source code to do so. #include <stdio.h> int main () { FILE * file = fopen ( "documents.txt" , "r" ); char line [ 256 ]; int i = 0 ; while ( fgets ( line , sizeof ( line ), file )) { i ++; if ( i == 9 ) { printf ( "%s" , line ); } } fclose ( file ); return 0 ; } Similarly if you want to read only the 1st/2nd/3rd/4th/5th or any other line then just run variable i till your desired line.   For example if i  want to read the 10th line of the same file if ( i == 10 ) { printf ( "%s" , line ); }

How to connect php to mysql database? Simple html comment box tutorial.

First thing first. You will need a little bit knowledge of web programming. Simple HTML will work. Now here is a step by step procedure on how to connect php to mysql database using local server. Lets create a simple comment box. And I will teach you how to do this on both Mac or windows/Linux. Here is a list of Applications you need before doing some coding. A text editor ( I would recommend Sublime text ) A local server software ( MAMP for mac , Xampp for windows/linux) Now that you have downloaded your local server software, install it and start the servers. If you are on mac follow the screenshot to start up the servers. Bravo ! Your computer is now a server (local though). If you are on windows/Linux follow the similar procedure to start your servers.  Now lets get into some simple coding. Since we are going to make a simple comment box, lets create two files "comment.php"and "comment_post.php" inside htdocs folder. Note : Your def...