What is the difference between PHP strings and MySQL strings when constructing queries?
When constructing queries in PHP, it is important to differentiate between PHP strings and MySQL strings. PHP strings are enclosed in double quotes ("") or single quotes (''), while MySQL strings are enclosed in single quotes (''). When passing PHP variables to a MySQL query, make sure to properly escape the variables to prevent SQL injection attacks.
// Example of constructing a query with PHP strings and MySQL strings
$name = "John";
$query = "SELECT * FROM users WHERE name = '$name'";
```
To properly handle PHP variables in a MySQL query, you can use prepared statements with parameter binding to ensure the variables are safely escaped. Here is an example using prepared statements:
```php
// Example of constructing a query with prepared statements
$name = "John";
$stmt = $mysqli->prepare("SELECT * FROM users WHERE name = ?");
$stmt->bind_param("s", $name);
$stmt->execute();
$result = $stmt->get_result();