为什么这个脚本不再起作用了?该脚本一直在使用以前的php版本(5.6之前)和数据库连接的默认编码:
include('connect-to-database-with-utf8.php');
header('Content-Type: image/png');
$im = imagecreatetruecolor(45, 22);
$text_color = imagecolorallocate($im, 255, 0, 0);
imagestring($im, 10, 5, 3, '1234', $text_color);
imagepng($im);
imagedestroy($im);
webbrowser表示图片无法显示,因为它包含错误......但如果我不通过直接将代码复制到文件中来使用任何包含,那么它将起作用。很奇怪。
包含文件' connect-to-database-with-utf8.php'包含:
$mysqli = new mysqli('db_host', 'db_user', 'db_password');
$mysqli->select_db('db_name');
// set charset to utf-8 for database requests
$mysqli->set_charset('utf-8');
$stmt = $mysqli->prepare('SET NAMES \'utf8\'');
$stmt->execute();
$stmt->close();
$stmt = $mysqli->prepare('SET CHARACTER SET \'utf8\'');
$stmt->execute();
$stmt->close();