I have a table that contains company names in this format: Name Name, and I have a search box that performs a MySQL query to return data based on the search string.
Let’s say my table data looks like this:
Company
----------
Name Name
Name Name
And so on and so forth, and my query looks like this:
$sql = "SELECT *
FROM scoreboard
WHERE codcliente = '".$q."'
OR nombre LIKE '%".$q."%'";
Now, everything works fine when I try: nam, Nam, Name, Name N, NAM, NAME, and NAME N. My question is why doesn’t it work with: name or name n? Obviously it might have to do with the mixed case in the table data, but if so, why does nam work?
Here’s the SHOW CREATE TABLE scoreboard result:
CREATE TABLE `scoreboard` (
`id` int(11) NOT NULL DEFAULT '0',
`codcliente` text,
`nombre` text,
`ejecutivo` text,
`banca_as400` text,
`banca_real` text,
`ingresos` varchar(20) DEFAULT NULL,
`ciiu` text,
`division` text,
`actividad` text,
`riesgo_industria` text,
`riesgo_cliente` text,
`fecha` date DEFAULT NULL,
`analista` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
To avoid case problems, compare them all in uppercase so you won’t be disturbed by case.
Seems hacky, but nevertheless reliable.