Displaying records in PHP from MySQL table

Displaying data from a table is a very common requirement and we can do this in various ways depending on the way it is required. We will start with a simple one to just display the records and then we will move to advance one like breaking the returned records to number of pages. We will start with simple displaying the records of this table.
id name class mark
1 John Deo Four 75
2 Max Ruin Three 85
3 Arnold Three 55
4 Krish Star Four 60
5 John Mike Four 60
6 Alex John Four 55
7 My John Rob Fifth 78

Before starting please ensure that you have connected to MySql database and also check the article on PHP MySQL query to know how to execute MySql queries by using PHP

Let us first start by storing the query in a variable and then executing it

MySQLI database connection file

Example : Object Oriented Style
<?Php
require "config.php";// Database connection file.

$query="select * from student LIMIT 0,5 ";
if ($result_set = $connection->query($query)) {
while($row = $result_set->fetch_array(MYSQLI_ASSOC)){
echo $row['id'],$row['name'],$row['class'],$row['mark']."<br>";
}
 $result_set->close();
}
?>
We used mysqli_query() function to get the array of records and then used WHILE loop to display record by record. You can see our php While to learn about loops. The code above will print name , class and mark records and you can see we have used <br> tag to give one line break after each record. This can be formatted well to display inside a table.
idnameclassmarksex
1John DeoFour75female
2Max RuinThree85male
3ArnoldThree55male
4Krish StarFour60female
5John MikeFour60female
Procedural style
<?Php
require "config.php";// Database connection file.

$query="select * from student LIMIT 0,5 ";
if ($result_set = mysqli_query($connection,$query)) {
while($row = $result_set->fetch_array(MYSQLI_ASSOC)){
echo $row['id'],$row['name'],$row['class'],$row['mark']."<br>";
}
 $result_set->close();
}
?>

Filtering records.

By adding SQL commands like WHERE conditions we can filter data as per our requirements. Let us find out the records of class Four only.
$query="select * from student WHERE class='Four'";

SQL WHERE Condition

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
Visitors Rating
Your Rating


Google+

mariel

16-02-2012

thank you...it works..
Simon

22-02-2012

Thanks dude, this tutorial helped me a lot.
Atar

25-05-2012

Hi can you tell me how to insert date month and year combine in databese
Venedict Francisco

07-09-2013

how can i post or display data information from the two different table?
subhendu

09-06-2015

You can always combine more than one table and display information. You can select multiple tables in a select statement and join more than two tables by using LEFT join



Check SQL section for more details.

Post Comment This is for short comments only. Use the forum for more discussions.




Subscribe to our mailing list

* indicates required
Subscribe to plus2net
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2017 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer