Skip to content
PHP Logo

Is it recommended to use a placeholder like "1=1" in the WHERE clause when dynamically building conditions in a PHP MySQL query?

When dynamically building conditions in a PHP MySQL query, it is not recommended to use a placeholder like "1=1" in the WHERE clause. This can lead to SQL injection vulnerabilities if user input is not properly sanitized. Instead, you should use prepared statements with placeholders to securely pass user input into the query.

php
// Example of using prepared statements with placeholders in a PHP MySQL query
$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');

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

// Prepare the statement
$stmt = $pdo->prepare("SELECT * FROM table WHERE column = :userInput");

// Bind the parameter
$stmt->bindParam(':userInput', $userInput);

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

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

// Output the results
print_r($results);