What is the function of the bindParam method in PHP?
The bindParam method in PHP is used to bind a parameter to a prepared statement. This method helps prevent SQL injection attacks by securely passing user input as parameters rather than directly inserting them into the SQL query. By using bindParam, the values are automatically sanitized and escaped before being executed in the database query. Example PHP code snippet:
// Assume $pdo is a PDO object connected to the database
// Prepare a SQL statement with a placeholder for a parameter
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
// Bind a value to the parameter
$username = 'john_doe';
$stmt->bindParam(':username', $username);
// Execute the prepared statement
$stmt->execute();
// Fetch the results
$results = $stmt->fetchAll();