Database Integration

PHP has several functions to work with databases. In this section, we'll focus on MySQL, a popular open-source relational database management system.

Connecting to MySQL Database

Here's an example of connecting to a MySQL database:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

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

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Creating a Table

Here's an example of creating a table in the database:

Inserting Data

This is how you can insert data into the database:

Retrieving Data

You can retrieve data from the database using SELECT statement:

Knowing how to interact with databases is crucial for backend web development.

Last updated