Tuesday 30 January 2024
Tuesday 17 October 2023
DATABASE CONNECTIVITY STEP BY STEP GUIDANCE FOR Beginners....
DATABASE CONNECTIVITY
To fetch data from a MySQL database and display it in an
HTML table format, you'll need to use a server-side scripting language (such as
PHP) to communicate with the database and generate the HTML content.
Step-by-Step guidance is as follows.
1.
Set Up Your Environment:
Download XAMPP - https://www.apachefriends.org/index.html
2.
Create a Database.
To Create Database: https://www.geeksforgeeks.org/how-to-create-a-new-database-in-phpmyadmin/
3.
Server side Scripting Language.
(PHP) Language: We require server side scripting language for
following task:
a. To connect to the database,
b. To retrieve data from database,
c. To generate HTML page.
4.
Establish Database Connection.
To establish a MySQL database connection in PHP script you
can use mysqli () extension.
<? php
$host = "your_host"; // Replace with your MySQL server hostname
$username = "your_username"; // Replace with your MySQL username
$password = "your_password"; // Replace with your MySQL password
$database = "your_database"; // Replace with your MySQL database name
// Create a connection to the MySQL database
$connection
= mysqli_connect ($host, $username, $password, $database);
// Check the connection
if
(!$connection) {
die("Connection failed: " .
mysqli_connect_error());
}
// You now have a valid database connection in the
$connection variable.
// Perform database operations, queries, etc. here
// Close the connection when you're done
mysqli_close
($connection);
?>
5.
Query the Database.
Write
a query as per the business logic to retrieve the data from database.
$query = "SELECT * FROM
your_table_name";
$result = mysqli_query ($connection, $query);
6.
Fetch and Format Data.
Now you can retrieve the data from the result set and
format it as HTML. To do this we can use the loop to iterate through the data and prepare
the table rows and cells.
echo
"<table>";
while ($row =
mysqli_fetch_assoc($result)) {
echo "<tr>";
echo
"<td>" . $row['column1'] . "</td>";
echo "<td>"
. $row['column2'] . "</td>";
// Add more columns as
needed
echo "</tr>";
}
echo
"</table>";
7.
Display in HTML.
You have to embed the PHP code that generates the HTML table
in your HTML file. Ensure that the HTML file has a proper file extension like .php
<!DOCTYPE html>
<html>
<head>
<title>MySQL to HTML Table</title>
</head>
<body>
<?php
// Your PHP code here
?>
</body>
</html>
8.
Style the Table by using a style sheet.
You can use CSS in your code to make it style better.
9.
Check the data which is displayed on a webpage is valid.
In the end, you have to check whether the data which are fetching from the table are valid or not.