views:

95

answers:

1

hello guys!

i need to perform a name search in the database based on a set of keywords. the result should be accent sensitive and case insensitive.

following the solution i found here plus few modifications to allow case insensitivity, i used the following code:

$sql = "SELECT name FROM table_names WHERE LOWER(name) LIKE _utf8 '%".strtolower($keyword)."%' COLLATE utf8_bin";

this does not work for me when the search keyword contains accents. but the strange thing is that this query works well in phpMyAdmin (but i read somewhere that this shouldn't be reliable). what could be the problem here?

UPDATE: i revised my code to:

$sql = "SELECT name FROM table_names WHERE LOWER(name) LIKE LOWER(_utf8 '%$keyword%') COLLATE utf8_bin";

but i still have no results.

thanks!

A: 

You can try to echo the sql being executed and check if the accents are there.

My guess they are not...

[EDIT]

Can you try this:

php.

<html>
<head>
</head>

<body>
<form action="" method="get">
<input name="name" type="text"/>
<input type="submit" value="go"/>
</form>

<?php
if (!empty($_GET['name'])) {
    $mysqli = new mysqli('localhost', 'root', '', 'test');
    $mysqli->set_charset('latin1');

    $_GET['name'] = utf8_encode($_GET['name']);

    if ($result = $mysqli->query('select * from dummy where LOWER(name) LIKE _utf8 \'%' . $_GET['name'] . '%\' COLLATE utf8_bin')) {
        while ($obj = $result->fetch_object()) { 
            var_dump($obj->name); 
        }
    }
    $mysqli->close();
}
?>
</body>
</html>

mysql: (doesn't matter)

CREATE TABLE `dummy` (
  `id` int(11) NOT NULL,
  `name` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin5$$

This one works for me...

sheeks06
i tried echoing it and the accents are there. the echoed statement was also the query i used to run in phpMyAdmin.
user398651
check my edited answer
sheeks06
thanks! but i can't set my table's collation to utf8_bin as it has to be case and accent insensitive. i only have one instance in which i have to make an accent sensitive comparison..
user398651
final answer :)
sheeks06
try utf8_encode. works perfectly
sheeks06
ok, thanks! will try first thing in the morning..
user398651
you're right! adding utf8_encode made it work. thanks!!
user398651