mysqli bind_param是罪魁祸首吗?无法保存像€这样的特殊字符


Is mysqli bind_param the culprit? Cannot save special characters like €

我正在尝试使用PHP/MMySQL创建一些页面。

我的数据库表被设置为utf8_general_ci,表列也被设置为。如果我使用phpmyadmin并手动插入一个页面,我可以键入我想要的所有€,它们将以€的形式插入。我也能完美地展示这一点。

然而,当我尝试使用mysqli插入页面时,欧元将被转换为"">

我没有做任何形式的转换。如果我在即将插入页面之前回显页面列字段,它仍然正确显示€。

代码:

$connection = new mysqli('localhost', 'root', '', 'some_db');
$query  = 'INSERT INTO page (ID, title, content) VALUES (?, ?, ?)';
$params = array('iss', 'NULL', 'test title', '€ some content');
$stmt = $connection->prepare($query);
call_user_func_array(array($stmt, 'bind_param'), by_ref($params));
$stmt->execute();

by_ref函数只做了一个引用,没有做其他任何操作。一切都很好,只是当它没有正确插入欧元时让我头疼。也许它不是bind_param,而是其他东西。但是我想知道答案!

更新:

多亏了Digital Precision,我发现我必须确保连接使用"utf8"。然而,我只需要在使用INSERT或UPDATE时设置它,因为SELECT确实在utf8中给了我结果。希望这能帮助

PHPMyAdmin可能会显式地将连接字符集设置为UTF-8,这就是为什么它可以通过管理界面而不是通过脚本工作。查看mysqli::set_charset了解更多详细信息。

$connection->set_charset('utf8');

编辑

尝试取出call_user_function:

$connection = new mysqli('localhost', 'root', '', 'some_db');
$query  = 'INSERT INTO page (ID, title, content) VALUES (?, ?, ?)';
//$params = array('iss', 'NULL', 'test title', '€ some content');
$stmt = $connection->prepare($query);
// Replace this
//call_user_func_array(array($stmt, 'bind_param'), by_ref($params));
// With this
$stmt->bind_param('iss', 'NULL', 'test title', '€ some content'); 
$stmt->execute();