TechTorch

Location:HOME > Technology > content

Technology

How to Fetch and Display Data from a Database in PHP

May 13, 2025Technology3326
How to Fetch and Display Data from a Database in PHP PHP is a powerful

How to Fetch and Display Data from a Database in PHP

PHP is a powerful server-side scripting language that can be used for managing database connections and querying data from a database. This guide will walk you through the essential steps to fetch and display data from a MySQL database using PHP. You will learn how to establish a connection, execute SQL queries, and present the retrieved data on a webpage.

1. Establishing a Connection to the Database

The first step in managing a MySQL database with PHP is to establish a connection. This can be done using the mysql_connect() or mysqli_connect() function. For simplicity, this example will use the older mysql_ functions, but it's recommended to use mysqli_ or PDO for better security.

Example Code:

$db  mysql_connect('hostname', 'username', 'password') or die('Could not connect: ' . mysql_error());

In this example, replace hostname, username, and password with your actual database credentials. The or die() statement will help youdebug the connection issues.

2. Selecting the Database

After establishing the connection, you need to select the database you want to work with. Use the mysql_select_db() function for this purpose.

Example Code:

mysql_select_db('database_name', $db) or die('Could not select database: ' . mysql_error());

Replace database_name with the name of your database.

3. Executing SQL Queries

Once you have a connection to your database, you can execute SQL queries using the mysql_query() function. This function is used to run an SQL statement on a MySQL database.

Example Code:

$sql  'SELECT column_names FROM table_name';
$query  mysql_query($sql, $db) or die('Query failed: ' . mysql_error());

In this example, column_names specifies the columns you want to retrieve, and table_name is the name of the table you are querying. The mysql_query() function returns a result set or false if the query fails.

4. Fetching Results and Displaying Data

After executing the SQL query, you can fetch the results using functions like mysql_fetch_assoc(), mysql_fetch_array(), or mysql_fetch_row(). These functions iterate through the result set and retrieve data row by row.

Example Code:

while ($row  mysql_fetch_assoc($query)) {
    echo $row['column_name'] . '
'; }

Replace column_name with the name of the column you are displaying. The echo statement will output each column value on a new line.

5. Closing the Database Connection

Finally, don't forget to close the database connection to free up system resources. Use the mysql_close() function for this purpose.

Example Code:

mysql_close($db);

Additional Tips:

Security: Note that the mysql_ functions are deprecated as of PHP 5.5.0 and removed in PHP 7.0.0. Consider using mysqli_ or the PDO extension for better security and performance.

Prepared Statements: Use prepared statements to prevent SQL injection attacks. They allow you to prepare an SQL statement and then execute it with parameters.

Result Handling: Always check if the query was successful and handle errors gracefully to ensure a good user experience.

By following these steps, you can effectively fetch and display data from a database in PHP. This foundational knowledge is critical for any web developer working with database-driven applications.