How can the use of prepared statements improve the security and readability of SQL queries in PHP?

Using prepared statements in PHP can improve security by preventing SQL injection attacks, as it separates the SQL query from the user input. This also enhances the readability of SQL queries by clearly separating the query structure from the input parameters. Prepared statements allow for better performance as they can be reused with different parameters.

// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare a SQL statement
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');

// Bind parameters
$stmt->bindParam(':username', $username);

// Execute the statement
$stmt->execute();

// Fetch results
$results = $stmt->fetchAll();