Jump to content
#StayAtHome ×
kk4iku

use keys from associative array in and update statement.

Recommended Posts

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

 

Share this post


Link to post
Share on other sites

The syntax for an "in" clause is

 

... IN ('aaa','bbb',...,'zzz')
Edited by Barand

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.