CakePHP - the request returns an empty field if it has a special character

I have what I consider to be a regular request in CakePHP - it works for all results, but when there is a special character in the field, the field is returned empty. It won't break - and it still gives me the rest of the fields - it's just one empty field.

Example:

$this->paginate = array( 'conditions' => array( 'Item.name != ' => '', ), ); $data = $this->paginate('Item'); 

This will return all the elements of my table (including the one that I thought had an empty name field) - but when I try to repeat the name on the page, it works for every element except one with a special character (e). I changed it to a normal "e" and it looks fine.

How can I return results even if they have a special character in their name? Thanks in advance!

+7
source share
2 answers

Make sure your database uses the correct encoding (ideally, UTF-8), and you configured Cake to use the same encoding in config/database.php :

 class DATABASE_CONFIG { public $default = array( ... 'encoding' => 'utf8' ); } 

If you have incorrect encoding, your application probably already stored garbage in the database, so make sure you check the correct data and / or the new database.

+8
source

These are probably fewer Cake related issues and PHP / MySQL related issues. (Others have already created the encoding, so I will skip this.)

Single quotes mean that the literal string is passed to MySQL: 'Item.name != ' => ''

PHP (a la Cake) probably parses this line literally. In fact, it can even parse it like this:

 "Item.name != " 

(don’t notice that there is nothing after the operand, and if it is the last in the SQL query, the query will not be an error, it will probably work anyway!)

When you were intended for testing:

 "Item.name != ''" 

(note that only single quotes are included in this line)

However, since you are not getting an error - and the rest of the data is pulling! - you probably want to edit this statement because your problem is more of syntax.

 'Item.name IS NOT NULL' 'Item.name <> ' => '' 'Item.name IS NOT' => '' 

Try to try.

http://dev.mysql.com/doc/refman/5.6/en/comparison-operators.html describing IS NOT NULL vs IS NOT (bool) vs <> and! = (not equal).

Hth :)

0
source

All Articles