Codeigniter DB or URI problem with unicode
I'm having a weird problem.
I have this code
$topic contains "海賊_(ONE_PIECE)" from URI /trends/about/海賊_(ONE_PIECE) I checked to echo $topic and it prints out 海賊_(ONE_PIECE)
$sql="SELECT wti.redirect_title FROM wikipedia_timeli开发者_运维知识库nes AS wti WHERE wti.redirect_title = ? LIMIT 1";
$query = $this->db->query($sql,array($topic));
if ($row = $query->result_array())
{
The problem is that this code returns $row to be an empty array
Array
(
[0] => Array
(
[redirect_title] =>
)
)
However, if I use this code (replacing ? with the actual value of $topic, it works perfectly
$sql="SELECT wti.redirect_title FROM wikipedia_timelines AS wti WHERE wti.redirect_title = '海賊_(ONE_PIECE)' LIMIT 1";
$query = $this->db->query($sql,array($topic));
if ($row = $query->result_array())
{
Replacing ? with {$topic} will not make it working too.
This problem only occurs when $topic contains ( ) if it doesn't have () it works fine
I wonder what is the problem. I suppose there is a problem with URI encoding, but I'm not sure how to fix it.
Please help me. Thank you
I'm pretty sure your problem is that query binding escapes your input values (im guessing it doesn't like brackets).
try building your sql string with concatenation, like:
$sql = "SELECT wti.redirect_title FROM wikipedia_timelines AS wti WHERE wti.redirect_title = '".$topic."' LIMIT 1";
$query = $this->db->query($sql);
and see if that helps. Or try the active record class, I use it for japanese characters all the time and it seems to work ok.
// EDIT:
To allow brackets in the URI, try and change application/config.config.php
and set
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-()';
also try urldecode($topic);
to get the kanji out.
精彩评论