sql - Merge Insert Into TABLE with an PHP ARRAY and for loop -


i have php array, coming database. array want insert different db/table. 2 fields (webinv_id,hostname) unique. update these table daily per script. if entry exist, must skip insert , should use update. that's working well. if run in php loop, insert or update first entry of array new table use

merge table ... 

for that. input array:

[0] => array     (         [ci_id] => 39778         [nodealias] => rt-2         [node] => 10.1.2.3         [serialnumber] => 8374378584         [vendorname] => cisco         [status] => active     )  [1] => array     (         [ci_id] => 72909         [nodealias] => rt-1         [node] => 10.1.1.3         [serialnumber] => 1276731237         [vendorname] => cisco         [status] => active     )... 

here loop try insert or update of devices.

for ($i = 0; $i < count($router); $i++) {      $sql = "merge devices using ( select "             . "'" . $router[$i]['ci_id'] . "' webinv_id,"             . "'" . $router[$i]['nodealias'] . "' device_name,"             . "'" . $router[$i]['node'] . "' node,"             . "'" . $router[$i]['serialnumber'] . "' serialnumber,"             . "'" . $router[$i]['vendorname'] . "' vendorname,"             . "'" . $router[$i]['status'] . "' status,"             . "current_timestamp device_insert dual     ) s on ('" . $router[$i]['ci_id'] . "' = s.webinv_id          or '" . $router[$i]['nodealias'] . "' = s.device_name)     when matched update set status = '" . $router[$i]['status'] . "', device_update = current_timestamp     when not matched insert(webinv_id,device_name,node,serialnumber,vendorname,status,device_insert)     values ('" . $router[$i]['ci_id'] . "',"             . "'" . $router[$i]['nodealias'] . "',"             . "'" . $router[$i]['node'] . "',"             . "'" . $router[$i]['serialnumber'] . "',"             . "'" . $router[$i]['vendorname'] . "',"             . "'" . $router[$i]['status'] . "',"             . "current_timestamp)";      $stid = oci_parse($gnedb, $sql);     oci_execute($stid);  // executes , commits } 

i've tried same code "oci_bind_by_name", result same, first entry used array. idea?

if using oci_bind_by_name understand use oracle database.

maybe should this:

$insert_query = "insert "; foreach($router $record) {   $fields = "";   $values = "";   foreach($record $field => $value) {     $fields .= $field . ($field != 'status' ? ',' : ""); // if in array last field status - if not might try figure out last column diferently or remove last ',' differently     $values .= "'" . $value . "'" . ($field != 'status' ? ',' : ""); // if in array last field status - if not might try figure out last column diferently or remove last ',' differently   }   $insert_query .= " devices (" . $fields . ") values (" . $values . ") "; } 

the above create query $insert_query , need send database. additional thing need figure out fields should quoted , doesn't. quoted of them, since oracle should able cast values correctly - need double check (i have no oracle available check :) )

the sql construction use can found here.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -