How can PHP developers ensure that sensitive data is not exposed when searching multiple columns in a database?
PHP developers can ensure that sensitive data is not exposed when searching multiple columns in a database by sanitizing user input and using prepared statements with parameterized queries. This prevents SQL injection attacks and ensures that data is securely retrieved from the database without exposing sensitive information.
// Sanitize user input
$searchTerm = filter_var($_POST['searchTerm'], FILTER_SANITIZE_STRING);
// Prepare a SQL query with parameterized statement
$stmt = $pdo->prepare("SELECT * FROM table_name WHERE column1 = :searchTerm OR column2 = :searchTerm");
$stmt->bindParam(':searchTerm', $searchTerm, PDO::PARAM_STR);
$stmt->execute();
// Fetch results securely
$results = $stmt->fetchAll();