PHP DB Connect: Easy Setup & Integration !

Опубликовано: 10 Октябрь 2024
на канале: DevVault
132
1

Introduction

PHP database connectivity is a vital aspect of web development as it allows web developers to connect their applications to a database management system (DBMS) such as MySQL, Oracle, or PostgreSQL. XAMPP is a free and open-source cross-platform web server solution stack that includes Apache, MySQL, PHP, and Perl. This article will provide a step-by-step guide on how to connect PHP to a database using XAMPP.

Setting up XAMPP:

To get started with XAMPP, you need to download and install it on your computer. You can download XAMPP from the Apache Friends website. Once you have downloaded XAMPP, follow the installation wizard to install it on your computer. After installation, open the XAMPP control panel and start the Apache and MySQL modules.

Creating a Database:

Before we can connect PHP to a database, we need to create a database. In this tutorial, we will use MySQL as our DBMS. Open your web browser and type http://localhost/phpmyadmin/ in the address bar. This will open the phpMyAdmin interface. Click on the "Databases" tab and enter a name for your new database in the "Create database" field. Click on the "Create" button to create the database.

Connecting to the Database:

To connect to the database, we need to use PHP's mysqli_connect() function. This function takes four parameters: the server name, username, password, and database name. Open your text editor and create a new PHP file. In this file, add the following code:

Code:
?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydatabase";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?
In the above code, we first define the server name, username, password, and database name. We then use the mysqli_connect() function to create a connection to the database. Finally, we check if the connection was successful and display a message indicating whether the connection was successful or not.

Executing SQL Queries:

Once we have established a connection to the database, we can execute SQL queries using PHP's mysqli_query() function. This function takes two parameters: the connection object and the SQL query. For example, to retrieve all the data from a table called "users", we can use the following code:

Code:
?php
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) greater then 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "br";
}
} else {
echo "0 results";
}

mysqli_close($conn);
?
In the above code, we first define an SQL query to retrieve all the data from the "users" table. We then use the mysqli_query() function to execute the query and store the results in a variable called $result. We then use the mysqli_num_rows() function to check if there are any rows returned by the query. If there are, we use a while loop and the mysqli_fetch_assoc() function to retrieve each row and display the data. Finally, we close the connection to the database using the mysqli_close() function.

Conclusion:

PHP database connectivity is a crucial aspect of web development, and XAMPP provides an excellent platform for developing PHP applications that interact with a database.

For Code: https://github.com/Umii010/PHP-Lectures
Quora Profile: https://www.quora.com/profile/UmerSha...

Do Like Subscribe and Share with Your Friends Keep Learning and Keep Exploring.