How can PHP developers prevent MySQL Injections in their code?

To prevent MySQL Injections in PHP code, developers should use prepared statements with parameterized queries instead of directly inserting user input into SQL queries. This helps to separate the data from the query structure, making it impossible for attackers to inject malicious code into the SQL query.

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

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

// Bind the parameter values
$stmt->bindParam(':username', $_POST['username']);

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

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