Jump to content

How do I get keys and values from an associative array into a SQL SERVER update statement


kk4iku

Recommended Posts

I am reading a file and get the information from it I need to update my SQL SERVER but I don't know how to get the values into the sql server update statement.

 

I use the below code to read the file and get the array. With help from an earlier post, I can get the Key value into my sql and it works fine but now I need the key value and the other values in an update statement. This code does a select and shows the order numbers.I will change the select to an update statement.How do I get item1 and item 2 into the sql update statement?

 

Update statement needed:

update data.dbo.SalesOrders set status="Item2 from array", assignment=""Item1 from array" where  ompsalesorderid in ($key_string)

My code:

<?php
$lines = [];
$fp    = fopen('CCDATA.TXT', 'r');
while (!feof($fp))
{
  $line  = fgets($fp);
  $index = substr($line, 69, 5);
   if(substr($line, 117, 2)=='AA' || substr($line, 117, 2)=='AC'){
  $lines[$index] = ['item1' => substr($line, 91, 10), 
                    'item2' => substr($line, 117, 2)];
$key_string = implode(',', array_keys($lines));


}


}
//print_r($lines);
fclose($fp);


$sql_update="select ompsalesorderid from  data.dbo.SalesOrders where  ompsalesorderid in ($key_string)";

$connect = odbc_connect("removed");
$result = odbc_exec($connect, $sql_update);
if(!$result){
exit("Error in SQL");
}
 while ($row = odbc_fetch_array($result))
{
   $orderid=$row['ompsalesorderid'];
   echo $orderid;
   echo "<br>";
 }


odbc_close($connect);




?>

output from the current code:

 

48580 
48585 
48587 
48588 
48592 
48593 

 

output from a print_r(lines):

 

Array ( [48588] => Array ( [item1] => 172689364 [item2] => AA ) [48580] => Array ( [item1] => 172689365 [item2] => AA ) [48592] => Array ( [item1] => 172689916 [item2] => AA ) [48587] => Array ( [item1] => 172689366 [item2] => AC ) [48597] => Array ( [item1] => 172680495 [item2] => AA ) [11238] => Array ( [item1] => 172680482 [item2] => AA ) [48593] => Array ( [item1] => 172683140 [item2] => AA ) [48594] => Array ( [item1] => 172683141 [item2] => AA ) [48598] => Array ( [item1] => 172683142 [item2] => AA ) [48603] => Array ( [item1] => 172686160 [item2] => AA ) [48585] => Array ( [item1] => 172686152 [item2] => AA ) [48601] => Array ( [item1] => 172686162 [item2] => AA ) [48615] => Array ( [item1] => 172693602 [item2] => AA ) [48618] => Array ( [item1] => 172693603 [item2] => AA ) [48621] => Array ( [item1] => 172696837 [item2] => AA ) [48623] => Array ( [item1] => 172696838 [item2] => AA ) [48625] => Array ( [item1] => 172696839 [item2] => AA ) [48628] => Array ( [item1] => 172699180 [item2] => AA ) [48629] => Array ( [item1] => 172699181 [item2] => AA ) [48631] => Array ( [item1] => 172699182 [item2] => AA ) [48636] => Array ( [item1] => 172692202 [item2] => AA ) [48632] => Array ( [item1] => 172692204 [item2] => AA ) [48633] => Array ( [item1] => 172692205 [item2] => AA ) [48634] => Array ( [item1] => 172692206 [item2] => AA ) [48635] => Array ( [item1] => 172692207 [item2] => AA ) ) 11238 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.