Delete NOT IN an array deletes all the rows, PHP Mysql -
i trying accomplish delete rows of table event id equal value(unique key) , ids not in array;
so lets event_id=5
has 4 rows(1,2,3,4)
, array has (1,2)
want delete 3,4
event id equal 5
.
to that: ->select id array , put id's array (seems working)
->delete rows except 1 comes select query(fails deletes rows of table).
$query = "select file_id files event_id=$event_id , name in ('$names')"; $result = $sql->query($query); //printf("$query: %s\n", $query); var_dump($query); //printf("\n"); if (!$result) { var_dump($result); printf("query failed: %s\n", $mysqli->error); sendresponse(417, json_encode("query failed")); exit; } //printf("\n"); $rows = array(); while($row = $result->fetch_row()) { $rows[]=$row; printf("\n"); } $result->close(); var_dump($rows); printf("\n"); $delete = join("', '",$rows); var_dump($delete); printf("\n"); //send delete request here $query ="delete files event_id=$event_id , file_id not in ('$delete')"; $result = $sql->query($query); //printf("$query: %s\n", $query); var_dump($query); printf("\n"); if (!$result) { var_dump($result); printf("\n"); printf("query failed: %s\n", $mysqli->error); sendresponse(417, json_encode("query failed")); exit; }
log:
string(143) "select file_id files event_id=7 , name in ('sample-1.pdf', '2012-lve-vegas-faq.pdf', 'sample-2.pdf', 'sample-3.pdf', 'sample.pdf')" array(5) { [0]=> array(1) { [0]=> string(2) "89" } [1]=> array(1) { [0]=> string(2) "90" } [2]=> array(1) { [0]=> string(2) "91" } [3]=> array(1) { [0]=> string(2) "92" } [4]=> array(1) { [0]=> string(2) "93" } } string(41) "array', 'array', 'array', 'array', 'array" string(99) "delete files event_id=7 , file_id not in ('array', 'array', 'array', 'array', 'array')"
you can see in log $delete = join("', '",$rows);
causes ('array', 'array', 'array', 'array', 'array')
not want,it should have been ('89','90', '91', '92', '93')
how can make work?
probably should $rows[]=$row['file_id']; or $rows[]=$row[0]; (depends on fetch method). however, more workable:
delete files event_id=$event_id , name not in ('$names')
Comments
Post a Comment