What are the advantages of switching to a database for storing user data in PHP?

Storing user data in a database in PHP offers several advantages over other methods, such as increased security, easier data manipulation, scalability, and better performance. By using a database, you can securely store sensitive user information, easily query and update data, handle large amounts of data efficiently, and ensure data integrity.

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

// Insert user data into the database
$sql = "INSERT INTO users (username, email, password) VALUES ('JohnDoe', 'johndoe@example.com', 'password123')";
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

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