In what scenarios can data stored in a database cause special characters to display as question marks in PHP?

Special characters may display as question marks in PHP when the character encoding of the database does not match the character encoding of the PHP script. To solve this issue, you can set the appropriate character encoding for the database connection in your PHP script using the `SET NAMES` query.

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

// Set the character encoding for the connection
$mysqli->query("SET NAMES 'utf8'");

// Retrieve data from the database
$result = $mysqli->query("SELECT * FROM table");

// Process the data
while ($row = $result->fetch_assoc()) {
    // Handle the data
}

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