What are the advantages of using a SQL database over a text file for storing data in a PHP script?

Using a SQL database over a text file for storing data in a PHP script offers advantages such as better organization of data with tables and relationships, faster data retrieval using SQL queries, built-in security features like user authentication and access control, and scalability for handling large amounts of data.

// Example PHP code snippet using a SQL database (MySQL) to store data

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

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

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