0

I've gotten to a point where I absolutely need some clean way to safely pass lists/arrays from php to SQL server stored procedures and table value functions. The PHP SQL server driver still does not support table valued parameters, according to Microsoft docs.

In another question, using XML was suggested as an alternative.

Does anyone have a code sample for passing the equivalent of a TVP using an XML stream and PHP PDO or another clean alternative?

1

2 Answers 2

1

The primitive, but foolproof solution, is to pass it as a delimited string, and use a SPLIT function in your proc to convert the string to a table that you can then JOIN to.

Google SQL SPLIT FUNCTION to get free cut-n-paste code.

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

Comments

0

You could use xml, but a better, more compact idea would be to use JSON.

$myArray = array("thing 1", "thing 2", "thing 3");
$sql = $pdo->prepare("INSERT INTO `table` (`array_data`) VALUES (:myArray)");
$sql->execute(array(":myArray"=>json_encode($myArray)));

Then when you pull the data back out of the database you can convert it back into an array with:

$myArray = json_decode($res['myArray'], true);

1 Comment

This is not exactly what I'm looking for. I'm not trying to store the array in the database, rather I want to read it into a stored procedure or user defined function as a parameter and be able to join it to other tables within sql server so I can use it as part of an inline select query if that makes any sense.

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.