2011-11-19 90 views
1

我正在尝试使用PHP/MySQL创建一些页面。是mysqli bind_param的罪魁祸首?无法保存特殊字符,例如€

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

然而,当我试图插入使用的mysqli的€网页将被转换为â,¬

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

代码:

$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,而是别的。我想知道答案!

更新:

由于数字精确我想通了,我必须确保连接打算用“UTF8”。不过,我只需要在使用INSERT或UPDATE时进行设置,因为SELECT确实给我返回了utf8的结果。希望这可以帮助别人

回答

3

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(); 
+0

并不能帮助我,当我插入,但使选择额外的怪异(即â,¬再次转换到页*) – Tessmore

+0

@Tessmore:尝试'$ connection-> get_charset()'并查看当前值是什么。 –

+0

@Tessmore:还有其他一些事情要尝试;尝试不使用绑定的查询,并尝试绑定在call_user_func_array之外。 –

相关问题