How can SQL injection vulnerabilities be addressed and prevented in PHP scripts, particularly when interacting with databases like MySQL?

SQL injection vulnerabilities can be addressed and prevented in PHP scripts by using prepared statements and parameterized queries when interacting with databases like MySQL. By binding parameters to SQL queries, it prevents malicious SQL code from being injected into the query, thus protecting against potential attacks.

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

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

// Bind the parameter to the placeholder
$stmt->bindParam(':username', $username);

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

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