CodeIgniter Forums

Full Version: html special characters & mysql
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]NikosV[/eluser]
ok.. here is my problem. i have got a functions that gets as input a keyword and searches into the database for that keyword. The problem is that when the keyword contains a special character (in my case a parenthesis) it doesn't return any results unless i have saved the characters in the database using their code For example:  0; for parenthesis. Is there a way so that i can avoid entering the code in the database?

thanks in advance.

El Forum

[eluser]NikosV[/eluser]
my database is utf8 if that helps.

El Forum

[eluser]kenjis[/eluser]
You stored "& #40;" in database? It's a bad practice.
It's better to store "(" in database.

El Forum

[eluser]NikosV[/eluser]
[quote author="Kenji @ CodeIgniter Users Group in Japan" date="1268373595"]You stored "& #40;" in database? It's a bad practice.
It's better to store "(" in database.[/quote]

but that's exactly my problem, when i store "(" my function doesn't get any results because the query is searching for "& #40;". How can i solve that?

El Forum

[eluser]kenjis[/eluser]
[quote author="NikosV" date="1268375182"][quote author="Kenji @ CodeIgniter Users Group in Japan" date="1268373595"]You stored "& #40;" in database? It's a bad practice.
It's better to store "(" in database.[/quote]

but that's exactly my problem, when i store "(" my function doesn't get any results because the query is searching for "& #40;". How can i solve that?[/quote]

How about chaning the query from searching "& #40;" to searching "(" ?

El Forum

[eluser]NikosV[/eluser]
ok maybe i didn't myself clear... while i retrieve data without special data codes from db.. when sending query are turning into special character codes...

let's say i have a model function get_best_resto() is like this:

Code:
function get_best_resto($startRow='',$n='')
    {
        $query = $this->db->query("select * from restaurant group by resto_rating DESC,resto_review DESC limit ".$startRow.",".$n);
        
        return $query;
    }


in the view file i am calling the above model function and i create link to a controller function restaurant/index() giving as input the resto_name :

Code:
<?php
                        $recommended_resto = $this->restaurant_model->get_best_resto(0,5);
                        
                        if($recommended_resto->num_rows()>0)
                        {
                            foreach($recommended_resto->result_array() as $row_reco)
                            {
                    ?>                
                    
                    <!-- RESTO 1-->
                    <div class="recommended_wrap">    
            
                        <div class="recommended_mid">                            
                            <p><a href="&lt;?php echo site_url('restaurant/index').">&lt;?php echo $row_reco['resto_name'];?&gt;</a></p>
                            
...                            
...


so when clicking the link the following controller function is loaded which is calling a model function is_resto_exist() to check if the keyword (resto_name) exists in the database:

Code:
function index($keyword='')
    {
            if(!empty($keyword))
        
        {    
            $is_resto_exist = $this->restaurant_model->is_resto_exist($keyword);        
                    
            
            if(isset($_SESSION['loggedin']) == 1)
            {            
                if($is_resto_exist)
                {
...

tha is_resto_exist model function is as follows:


Code:
function is_resto_exist($resto_name)
    {
        $this->db->where('resto_name',$resto_name);
        
        $query = $this->db->get('restaurant');
        
        if($query->num_rows() > 0)
        {
            return true;
        }else{
            return false;
        }
    }




and here is the problem: when keyword containing special characters (parenthesis in my case) cannot find it in the database. i used the CI profiler to check why is that happening, as it is the same sting that it retrieves from the database. and i realized that while it recieves the string from the database in the form "restaurant (whatever)", in the query it sends is in the form "restaurant & #40 the & #41". The problem is only solved when saving in the database the special character codes.
so...can anyone tell me, what i am missing here? how can i solve this problem?

El Forum

[eluser]kenjis[/eluser]
[quote author="NikosV" date="1268406200"]ok maybe i didn't myself clear... while i
Code:
function index($keyword='')
    {
            if(!empty($keyword))
        
        {    
            $is_resto_exist = $this->restaurant_model->is_resto_exist($keyword);        
                    
            
            if(isset($_SESSION['loggedin']) == 1)
            {            
                if($is_resto_exist)
                {
...
[/quote]

CI converts the string in $keyword to html entity.

How about below?

Code:
function index($keyword='')
    {
            $keyword = html_entity_decode($keyword);
            if(!empty($keyword))

El Forum

[eluser]NikosV[/eluser]
you were right... works fine now. Thanks :-)