pass variable into where mysql

49 views Asked by At

Cannot get the query to run, throwing error on the @searchin variable. Probably very simple but cannot see it.

set @search = "chip";
set @searchin = "CompanyName";

select * from con_search where @searchin like concat ('%',@search,'%')
1

There are 1 answers

0
Neo On BEST ANSWER

This will work, however you should sanatize the data going into it

set @search = 'chip';
set @searchin = 'CompanyName';
set @SQL = CONCAT("SELECT * FROM con_search WHERE `", @searchin, "` LIKE CONCAT('%'", @search, "'%');";
PREPARE stmt1 FROM @SQL;
EXECUTE stmt1;