What are the advantages and disadvantages of using a relational database like MySQL over file handling methods for data storage in PHP?

Using a relational database like MySQL for data storage in PHP offers advantages such as data integrity, scalability, and the ability to perform complex queries. However, it requires more setup and maintenance compared to simple file handling methods. Additionally, relational databases may have a higher upfront cost in terms of resources and time.

// Example PHP code snippet using MySQL for data storage

// Connect to 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);
}

// Perform SQL queries
$sql = "SELECT * FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}

// Close connection
$conn->close();