How does using a DBMS compare to handling CSV files in PHP for data storage and retrieval purposes?

Using a DBMS for data storage and retrieval provides a more efficient and organized way to manage data compared to handling CSV files in PHP. DBMS allows for easier querying, indexing, and data manipulation, while CSV files require manual parsing and processing. Additionally, DBMS offers better scalability and data integrity features.

// Example of connecting to a MySQL database and retrieving data
$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);
}

// Select data from a table
$sql = "SELECT id, name, email 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"]. " - Email: " . $row["email"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();