To pass an array of strings as a parameter to a MySQL stored routine, you can use a prepared statement and build the query string using the CONCAT() function.
DELIMITER $$ CREATE PROCEDURE GetFruits(IN fruitArray VARCHAR(255)) BEGIN SET @sql = CONCAT('SELECT * FROM Fruits WHERE Name IN (', fruitArray, ')'); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; END $$ DELIMITER ;
How to use:
SET @fruitArray = '\'apple\',\'banana\''; CALL GetFruits(@fruitArray);
This method will create a temporary table with the fruit names and then use a query to select the corresponding rows from the Fruits table.
The above is the detailed content of How to Pass a String Array to a MySQL Stored Procedure?. For more information, please follow other related articles on the PHP Chinese website!