What are the potential security risks associated with PHP scripts and how can they be mitigated?

One potential security risk associated with PHP scripts is SQL injection, where malicious users can manipulate SQL queries through user input. This risk can be mitigated by using prepared statements with parameterized queries to prevent user input from being directly interpreted as SQL commands. Example PHP code snippet implementing this fix:

// Establish database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

// Prepare SQL statement with parameterized query
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);

// Set username variable from user input
$username = $_POST['username'];

// Execute query
$stmt->execute();
$result = $stmt->get_result();

// Process query results
while ($row = $result->fetch_assoc()) {
    // Do something with the results
}

// Close statement and connection
$stmt->close();
$conn->close();