What are some common issues with character encoding when working with PHP and external databases like OpenGeoDB?
Common issues with character encoding when working with PHP and external databases like OpenGeoDB include mismatched character sets between the database and PHP, resulting in garbled or incorrectly displayed text. To solve this issue, you can set the character encoding in PHP to match the database's encoding using the `set_charset()` method in MySQLi or `PDO::exec('SET NAMES utf8')` in PDO.
// Using MySQLi
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$mysqli->set_charset("utf8");
// Using PDO
try {
$pdo = new PDO("mysql:host=localhost;dbname=database", "username", "password");
$pdo->exec("SET NAMES utf8");
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}