I have a script that uses a database table as a buffer.
It first pulls a list of objects from another table, then runs a loop that calls an API for each object. For each API answer I create an insert statement into the buffer table with the current iteration object's key and corresponding data and a delete statement for that object's key. Then I first run a delete to get that object's records out of the table and then insert the new and fresh data.
This happens about every 5 minutes and the script runs for about 2 minutes. Issue I'm running into that I get all the elements deleted from the table. I checked the data with R studio, it fluctuates about every 5 minutes.
Since it deletes on every object's call, if this delete caused the issue, the table would be empty all the time. If everything runs sequentially.
Here is the function that runs a call for the single object:
function singleProp( $pId , $wispId , $nasId , $req){
$res_j = apiCall( $wispId , $nasId );
$count = get_value_by_tag($res_j , "COUNT" );
$aps = array();
$no_down = 0;
$db_con = new db_connector();
$db_con->connect();
$stmt_d = $db_con->dblink->prepare( "DELETE FROM ap_down WHERE prop_id = ? ;" );
$stmt_d->bind_param("i",$pId);
$stmt_d->execute();
$stmt_i = $db_con->dblink->prepare("INSERT INTO ap_down ( prop_id , ip , prop_type , description , time ) VALUES (?,?,?,?,now());");
for( $i = 1 ; $i <= $count ; $i++ ){
if( get_value_by_tag($res_j , "APSTATUS" , $i ) == "DOWN" ){
$stmt_i->bind_param("issd" , $pId , get_value_by_tag( $res_j , "ACCESSPOINTS" , $i ) , $req , get_value_by_tag( $res_j , "DESCRIPTION" , $i ) );
$stmt_i->execute();
$no_down ++ ;
}
}
}
Any ideas, suggestions, other data you guys might want to see?
EDIT: using prepared statement since it's safer and makes it easier to read. EDIT: Code indentation.... because important.