Begin Web Programming with PHP & MySQL

Advertisements



Displaying data from MySQL

PHP is a server-side scripting language designed specifically for web development. PHP can manipulate data in the MySQL database using database connection. To display the table data it is best to use HTML.

"Select * from the table-name" is the basic MySQL query which will tell the PHP script to select all the data from the specified database table. After the query is executed, we take the result set to an array. We can fetch the data from the array and show it in HTML as output.

Following code shows the details from the database table to an HTML table.

<?php
$connection = mysqli_connect("localhost","root","","college"); // creating connection
$query = "SELECT * FROM student"; // Select query
$result = mysqli_query($connection, $query); // executing the query
echo '<table border="1">';
echo '<tr>';
echo '<th>Id</th>';
echo '<th>Name</th>';
echo '<th>Date of Birth</th>';
echo '<th>Course</th>';
echo '</tr>';
while($row_data = mysqli_fetch_array($result))
{
echo '<tr>';
echo '<td>'.$row_data['id'].'</td>';
echo '<td>'.$row_data['name'].'</td>';
echo '<td>'.$row_data['date_of_birth'].'</td>';
echo '<td>'.$row_data['course'].'</td>';
echo '</tr>';
}
echo '</table>';
mysqli_close($connection); // Close the connection to the database.
?>