What are the potential advantages of using a MySQL database instead of manipulating text files in PHP?

Using a MySQL database instead of manipulating text files in PHP offers several advantages such as improved data organization, faster data retrieval, easier data manipulation through SQL queries, better data security with user authentication and permissions, and scalability for handling large amounts of data.

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

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

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