kk4iku Posted September 21, 2017 Share Posted September 21, 2017 I am pulling values from a text file and putting them in an associative array and then filtering them to get what I need. I would then like to use those key values in my MSSQL update statement. But I am not sure how to convey that to my MSSQL statement. How would I do that? <?php $lines = []; $fp = fopen('sample.txt', 'r'); while (!feof($fp)) { $line = fgets($fp); $lines[substr($line, 69, 5)] = substr($line, 117, 2); } fclose($fp); $order=array_filter($lines,function($AA){ return $AA=='AA';} ); $sql_update="update salesorders set ompOrderApproval= -1 where order_id in $orders_array_key_place_holder" $connect = odbc_connect("1234"); $update = odbc_exec($connect, $sql_update); odbc_close($connect); ?> Quote Link to comment https://forums.phpfreaks.com/topic/305076-use-keys-from-associative-array-in-and-update-statement/ Share on other sites More sharing options...
Barand Posted September 21, 2017 Share Posted September 21, 2017 (edited) The syntax for an "in" clause is ... IN ('aaa','bbb',...,'zzz') Edited September 21, 2017 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/305076-use-keys-from-associative-array-in-and-update-statement/#findComment-1551734 Share on other sites More sharing options...
Solution Sepodati Posted September 21, 2017 Solution Share Posted September 21, 2017 You can use array_keys() to get the keys from order and then build your IN clause using implode $key_string = implode(',', array_keys($order); $sql_update="update salesorders set ompOrderApproval= -1 where order_id in ({$key_string})"; Quote Link to comment https://forums.phpfreaks.com/topic/305076-use-keys-from-associative-array-in-and-update-statement/#findComment-1551737 Share on other sites More sharing options...
Barand Posted September 21, 2017 Share Posted September 21, 2017 Assuming that's where the keys are. He hasn't given that information - we have to guess. Quote Link to comment https://forums.phpfreaks.com/topic/305076-use-keys-from-associative-array-in-and-update-statement/#findComment-1551740 Share on other sites More sharing options...
kk4iku Posted September 21, 2017 Author Share Posted September 21, 2017 I update my code to use the implode and I changed my sql statement to test and I get the correct results. thanks $lines = []; $fp = fopen('sample.txt', 'r'); while (!feof($fp)) { $line = fgets($fp); $lines[substr($line, 69, 5)] = substr($line, 117, 2); } fclose($fp); $order=array_filter($lines,function($AA){ return $AA=='AA';} ); $key_string = implode(',', array_keys($order)); //print_r($key_string); $sql_update="select ompsalesorderid from .SalesOrders where ompsalesorderid in ($key_string)"; $connect = odbc_connect("1234"); $result = odbc_exec($connect, $sql_update); if(!$result){ exit("Error in SQL"); } while ($row = odbc_fetch_array($result)) { $orderid=$row['ompsalesorderid']; echo $orderid; } odbc_close($connect); Quote Link to comment https://forums.phpfreaks.com/topic/305076-use-keys-from-associative-array-in-and-update-statement/#findComment-1551754 Share on other sites More sharing options...
Sepodati Posted September 22, 2017 Share Posted September 22, 2017 Sometimes the guess is right. Quote Link to comment https://forums.phpfreaks.com/topic/305076-use-keys-from-associative-array-in-and-update-statement/#findComment-1551769 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.