问题
Been struggling with this thing for hours now. Hopefully someone could help me out.
Trying to get data from mysql based on first and last name. Everything works just fine except when there is special characters like ä or ö.
I have the profiler on and query looks like this:
SELECT mail, address, title FROM users WHERE firstname='teemu' AND lastname='sel%C3%A4nne'
And it should be:
SELECT mail, address, title FROM users WHERE firstname='teemu' AND lastname='selänne'
In my model it's like this:
$sql = "SELECT mail, address, title FROM users WHERE firstname=? AND lastname=?";
How can I fix this? Thank you!
Controller
public function edit()
{
$this->load->model('p_model');
$this->load->view('edit',$data);
}
public function ajax_p($first,$last)
{
$this->load->model('p_model');
$data['info'] = $this->p_model->pInfo($first,$last);
$this->load->view('ajax/p',$data);
}
回答1:
Make sure you do not forget to urldecode the post values. What might be happening is that you use $_POST instead of the codeigniter function $this->input->post(); $_POST won't urldecode the parameters for you while the codeigniter function does.
So verify that the variable you pass to your query really is selänne and not sel%C3%A4nne
And if it is sel%C3%A4nne, use urldecode() or $this->input->post()
回答2:
Have you utf8_general_ci in database ? And your file is encoded as utf8 ?
回答3:
try putting your special characters in config like
$config['permitted_uri_chars'] = 'a-z 0-9~%.:@_\-ä';
and make sure your db is utf8_general_ci is your Server connection collation.
来源:https://stackoverflow.com/questions/21995707/codeigniter-mysql-query-with-special-characters