Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
 
what you tried so far? –  Nomi yesterday

2 Answers

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

share|improve this answer
 
this will not work, i edited the post, please see? –  Bhawin Parkeria yesterday
 
Refer to this link this might help you - stackoverflow.com/questions/17330557/… –  Praveen yesterday

Use foreach loop:

foreach(condition)
{
  //insert query
  //Execute Query
}

For more help paste code

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.