What are the potential benefits of using a database instead of text files for storing data in PHP?

Using a database instead of text files for storing data in PHP offers several benefits such as improved data organization, faster data retrieval, better data security, and easier data manipulation through SQL queries.

// Connect to a MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

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

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