What are the potential pitfalls of using single or double quotes in SQL queries in PHP, and how can they be avoided?
When writing SQL queries in PHP, using single or double quotes can lead to syntax errors or SQL injection vulnerabilities. To avoid this, it's recommended to use prepared statements with placeholders for variables in the query. This helps sanitize user input and prevent malicious SQL injection attacks.
// Example of using prepared statements to avoid SQL injection
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
$name = $_POST['name'];
$email = $_POST['email'];
$stmt = $pdo->prepare('INSERT INTO users (name, email) VALUES (:name, :email)');
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
$stmt->execute();