In PHP, what are the potential security risks associated with not properly sanitizing user input before using it as variables in SQL queries?

When user input is not properly sanitized before being used in SQL queries, it can lead to SQL injection attacks where malicious SQL code is injected into the query. This can allow attackers to manipulate the database, steal data, or even delete information. To prevent this, it is important to always sanitize user input by using prepared statements or parameterized queries.

// Example of using prepared statements to sanitize user input in PHP

// Assuming $conn is a valid database connection

// User input
$user_input = $_POST['user_input'];

// Prepare a SQL statement
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");

// Bind parameters
$stmt->bind_param("s", $user_input);

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

// Get results
$result = $stmt->get_result();

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

// Close the statement
$stmt->close();