0

I have some special characters stored in a MySQL database. Specifically ®. I want to SELECT all entries with these characters. I am currently using this statement:

mysql_query("SELECT * FROM table WHERE description LIKE '%®%' OR name LIKE '%®%'");

It returns 0 entries. When i try something with %a% it returns a lot so I know everything is right, it is just some kind of a charset problem or something.

When I echo "®" it returns "å¨".

Also when I do the exact same query in phpmyadmin it works properly. Please help!

TJE
  • 240
  • 1
  • 4
  • 14
  • 4
    You should stop using `mysql_*` functions. They're being deprecated. Instead use [PDO](http://php.net/manual/en/book.pdo.php) (supported as of PHP 5.1) or [mysqli](http://php.net/manual/en/book.mysqli.php) (supported as of PHP 4.1). If you're not sure which one to use, [read this SO article](http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons). – Matt Aug 03 '12 at 16:39
  • I think the db is fine, and you actually have an encoding problem on the PHP end. – bfavaretto Aug 03 '12 at 16:40
  • 2
    ® is a 'replacement character', and as such not recognizable by your database, which most likely has the å character stored. Try setting the connection encoding to the database encoding (maybe UTF-8) and then try again. Also, try searching for that 'proper character', instead of the 'replacement' one. Because THAT character seems like Multibyte gone Ascii to me. – ATaylor Aug 03 '12 at 16:42
  • No, the actual entries in the database have those exact characters. I looked in phpmyadmin. It's strange how it is not selecting them. Is there anyway to tell PHP not to convert the characters and leave them as is? – TJE Aug 03 '12 at 17:12
  • I checked the database again, and all the entries that show up with ® are actually just a bunch of random numbers and letters. How can i search for this with PHP? – TJE Aug 03 '12 at 17:27

1 Answers1

1

Read this its very help full to you

Just simply add those symbols to your text, and execute it as SQL query:

INSERT INTO tbl_name VALUES ("Here's my text: ©®");

When you want to display it one the website don't do anything with these symbols (but remember to escape at least <, >, & (using htmlspecialchars()) cause those has special meaning in XML/SGML (HTML) documents)

PS. Also remember to escape text passed to SQL query using mysql_real_escape_string() to avoid any SQL Injection problems. If your server has magic_quotes_gpc enabled disable it or at least filter your GET/POST/COOKIE data to its raw value. You should always consciously escape values. EDIT:

According to your comment... I don't remember whether magic_quotes_gpc are enabled by default but you can easily undone magic quotes effect. Just on the very beginning of your PHP code add something like this:

if (get_magic_quotes_gpc()) {
  array_walk_recursive($_GET, 'stripslashes');
  array_walk_recursive($_POST, 'stripslashes');
  array_walk_recursive($_COOKIE, 'stripslashes');
}

Now each GPC value should be always raw - without quotes - so you have to escape it manually before passing any variable into query.

Abid Hussain
  • 7,724
  • 3
  • 35
  • 53