Are there specific functions or methods in PHP that can help with encoding and decoding special characters for SQL queries?

Special characters in SQL queries can cause syntax errors or security vulnerabilities if not properly encoded. To prevent this, you can use the `mysqli_real_escape_string()` function in PHP to escape special characters before including them in SQL queries. This function ensures that the special characters are properly handled and do not affect the query execution.

// Connect to the database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Check connection
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Escape special characters before using them in SQL query
$name = mysqli_real_escape_string($mysqli, $name);

// Execute the SQL query with the escaped special characters
$sql = "SELECT * FROM users WHERE name = '$name'";
$result = $mysqli->query($sql);

// Process the query result
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Name: " . $row["name"];
    }
} else {
    echo "0 results";
}

// Close the database connection
$mysqli->close();