PHP 5.5, MySQL 5.6
Trying to create a stored procedure that implements the use of Wildcards to search a field which has options which are separated by |.
Trying to convert the following embedded query into a stored procedure:
$keywords = "Jack";
$sql='select * FROM dealers where searchPhrases LIKE "%|'.$keywords.'|%"';
$res=mysql_query($sql);
Here is stored procedure Option 1, these are called using PHP PDO, trying to update the project by removing the use of mysql_query from the project.
DROP PROCEDURE IF EXISTS `getDealerInfo`//
CREATE PROCEDURE `getDealerInfo`(
IN _search VARCHAR(100)
)
BEGIN
SET @query = CONCAT(
'select *
FROM dealers
where searchPhrases LIKE "%|'
,_search
,'|%"');
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END//
Here is procedure 2:
DROP PROCEDURE IF EXISTS `getDealerInfo2`//
CREATE PROCEDURE `getDealerInfo2`(
IN _search VARCHAR(100)
)
BEGIN
select *
FROM dealers
where searchPhrases LIKE _search;
END//
I am testing both out using mysql console and for each case I get 0 results.
Call getDealerInfo("Jack");
Call getDealerInfo2("%|Jack|%");
Both of these queries in mysql console return 0 records, but if I use the PHP above I get back at least 1 record for the query. How can I fix my stored procedure so that the %, wildcard character, works?
Solution - Stored Procedure 1 is correct. Thank you @Ravinder. A simple error in judgment. I modified the record in the wrong database.
Another solution to get Procedure 2 to work would be to change the line:
where searchPhrases LIKE _search;
to
where searchPhrases LIKE CONCAT('%|',_search,'|%');
You would then call the procedure with:
Call getDealerInfo("Jack");