shaddf Posted September 18, 2015 Share Posted September 18, 2015 i have this array: test={'dog','cow','shee'}; how can I pass it to a stored procedure: call sp_array(in i_arr1 text); what data type should I use? Iam currently looping through and multi executing the procedure in php to pass the values one by one .But is there a way to pass it to the procedure and then assign it to a cursor in sql. Quote Link to comment https://forums.phpfreaks.com/topic/298214-how-to-pass-an-array-to-mysql-stored-procedure/ Share on other sites More sharing options...
Destramic Posted September 18, 2015 Share Posted September 18, 2015 do you mean cache? any you array should be: $test = array( 'dog', 'cow', 'shee' ); or are you using a template engine like smarty or some other nonsense? Quote Link to comment https://forums.phpfreaks.com/topic/298214-how-to-pass-an-array-to-mysql-stored-procedure/#findComment-1521113 Share on other sites More sharing options...
QuickOldCar Posted September 18, 2015 Share Posted September 18, 2015 You can use list() or implode() them and comma separated If happened to pass them through a loop can also build a string using concatenating assignment operator .= Quote Link to comment https://forums.phpfreaks.com/topic/298214-how-to-pass-an-array-to-mysql-stored-procedure/#findComment-1521114 Share on other sites More sharing options...
shaddf Posted September 19, 2015 Author Share Posted September 19, 2015 You can use list() or implode() them and comma separated If happened to pass them through a loop can also build a string using concatenating assignment operator .= Sorry but ,i may have an array containing a sentences like "this is your day.Go for it","You are doing it well","Keep it up!".how caan I pass that array to a stored procedure.so that I can assign it to a cursor in the procedure Quote Link to comment https://forums.phpfreaks.com/topic/298214-how-to-pass-an-array-to-mysql-stored-procedure/#findComment-1521165 Share on other sites More sharing options...
QuickOldCar Posted September 19, 2015 Share Posted September 19, 2015 Arrays in stored procedures are not supported and can get messy. What you should do is normalization in a relational database instead. Splitting your data first and importing it into a database the way you intend to use it. Split the array prior and insert each as it's own id while associating it to whatever user/group/values id another table you intend on keeping it associated to. If insist on doing it as trying to save arrays, you have to come up with your own method that can work for you. Usually can pass a comma delimited string or split the data a delimiter would possibly never use such as a double pipe || Pass the string and store as VARCHAR(255) to the stored procedure. Inside the procedure you can split the string back into its elements. Can also look into serialize and unserialize Quote Link to comment https://forums.phpfreaks.com/topic/298214-how-to-pass-an-array-to-mysql-stored-procedure/#findComment-1521171 Share on other sites More sharing options...
shaddf Posted September 19, 2015 Author Share Posted September 19, 2015 Arrays in stored procedures are not supported and can get messy. What you should do is normalization in a relational database instead. Splitting your data first and importing it into a database the way you intend to use it. Split the array prior and insert each as it's own id while associating it to whatever user/group/values id another table you intend on keeping it associated to. If insist on doing it as trying to save arrays, you have to come up with your own method that can work for you. Usually can pass a comma delimited string or split the data a delimiter would possibly never use such as a double pipe || Pass the string and store as VARCHAR(255) to the stored procedure. Inside the procedure you can split the string back into its elements. Can also look into serialize and unserialize Thanks for the double pipe idea.how can i split the string back and assign it to a cursor in the stored procedure. Quote Link to comment https://forums.phpfreaks.com/topic/298214-how-to-pass-an-array-to-mysql-stored-procedure/#findComment-1521179 Share on other sites More sharing options...
QuickOldCar Posted September 20, 2015 Share Posted September 20, 2015 using explode() will explode the string back into an array at whatever delimeter use $array = explode("||",$data); Quote Link to comment https://forums.phpfreaks.com/topic/298214-how-to-pass-an-array-to-mysql-stored-procedure/#findComment-1521188 Share on other sites More sharing options...
Barand Posted September 20, 2015 Share Posted September 20, 2015 You might have to write the array contents to separate rows in a temporary table Quote Link to comment https://forums.phpfreaks.com/topic/298214-how-to-pass-an-array-to-mysql-stored-procedure/#findComment-1521194 Share on other sites More sharing options...
shaddf Posted September 22, 2015 Author Share Posted September 22, 2015 do you mean cache? any you array should be: $test = array( 'dog', 'cow', 'shee' ); or are you using a template engine like smarty or some other nonsense? No ido not mean cache.what I want is to pass a php array variable to a mysql stored procedure from php Quote Link to comment https://forums.phpfreaks.com/topic/298214-how-to-pass-an-array-to-mysql-stored-procedure/#findComment-1521351 Share on other sites More sharing options...
shaddf Posted September 22, 2015 Author Share Posted September 22, 2015 using explode() will explode the string back into an array at whatever delimeter use $array = explode("||",$data); Is the explode() available in mysql. Quote Link to comment https://forums.phpfreaks.com/topic/298214-how-to-pass-an-array-to-mysql-stored-procedure/#findComment-1521352 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.