php - Filter out unset mysqli_query -
i trying dynamically set sql's based on variable values..
so if have
$somevariable = "somevalue" set $sql , $a1 = mysqli_query($con, $sql1) $somevariable2 = "somevalue2" set $sql2 , $a2 = mysqli_query($con, $sql2) and on..
now want run these queries , if of them false want rollback
so here have done..
mysqli_autocommit($con, false); if ($a1 , $a2 , $a3 , $a4 , $a5 , $a6) { mysqli_commit($con); } else { mysqli_rollback($con); } now problem $a can number , number in between might not there $a1 , $a7 or that.
so if ($a1 , $a2 , $a3 , $a4 , $a5 , $a6) not commit because 1 of variables not there.
if give ($a1 or $a2 or $a3 or $a4 or $a5 or $a6) runs first query , commit.
so how can give many variables have set , filter out unset variables before give 'and' commit
i have tried
$tmp = array($a1,$a2,$a3,$a4,$a5,$a6,$a7,$a8,$a9,$a10); $filtered = array_filter($tmp); $one = implode("and", $filtered) if ($one) { mysqli_commit($con); } else { mysqli_rollback($con); } but wont work either.. idea? thanks.
you don't need use explicit variables each interaction unless want them later. can use [] append result array aren't leaving unassigned members, , use array_reduce evaluate.
function bool_and($a,$b) { return $a , $b; } $result_array = array(); $result_array[] = mysqli_query($con, $sql1) $result_array[] = mysqli_query($con, $sql2) $result_array[] = mysqli_query($con, $sql7) if (array_reduce($result_array,"bool_and",true)) { mysqli_commit($con); } else { mysqli_rollback($con); }
Comments
Post a Comment