Search code examples
phpmysqlcodeigniterspecial-characters

Codeigniter mysql query with special characters


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);
    }

Solution

  • 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()