What are the potential pitfalls of using hardcoded values in SQL queries when retrieving data from a database in PHP?

Hardcoding values in SQL queries can lead to SQL injection attacks, as malicious users can manipulate the input to execute unauthorized SQL commands. To prevent this, it is recommended to use prepared statements with parameterized queries in PHP. This way, input values are treated as data and not executable code, making the queries secure.

// Using prepared statements with parameterized queries to prevent SQL injection

// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare a statement with a placeholder for the input value
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');

// Bind the input value to the placeholder
$username = $_POST['username'];
$stmt->bindParam(':username', $username);

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

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