1

I have a table in which i want to insert multiple rows having values from a php array , now i can't figure out how to pass an array in stored procedure .

Example-: i have a php array having names ['sqlite','mysql','sql']

now what i want is to send this array to stored procedure and loop through the array taking one value at a time and inserting into the database table.

1
  • what you tried so far? Commented Nov 18, 2013 at 9:23

3 Answers 3

2

You can pass a string with your list and use a prepared statements to run a query, e.g. -

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);

Sign up to request clarification or add additional context in comments.

1 Comment

Refer to this link this might help you - stackoverflow.com/questions/17330557/…
1
SELECT *

FROM fruits

WHERE FIND_IN_SET ( name, fruit_array );

Hope this will help you..

Comments

0

Another Solution:

Use foreach loop:

foreach(condition)
{
  //Create insert query string
}

//insert query
//Execute Query

For more help paste code

1 Comment

@leandronn I have just suggested another solution to insert multiple values in a single query. Please check edited answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.