drkjzk3359 2013-05-14 17:13
浏览 36
已采纳

删除NOT IN数组删除所有行,PHP Mysql

I am trying to accomplish delete all the rows of a table where event id is equal to some value(unique key) and ids not in my array;

So lets say event_id=5 has 4 rows(1,2,3,4) and I my array has (1,2) I want to delete 3,4 where event id is equal to 5.

To do that: ->Select ID from an array and put id's to another array (seems working)

->Delete all the rows except the one comes with select query(fails deletes all the rows of the table).

$query = "SELECT file_id FROM FILES WHERE event_id=$event_id AND name IN ('$names')";
    $result = $sql->query($query); 
    //printf("$query: %s
", $query);
    var_dump($query);    
    //printf("
");
    if (!$result) {
        var_dump($result);
        printf("Query failed: %s
", $mysqli->error);
        sendResponse(417, json_encode("Query failed"));

    exit;
    }
    //printf("
");
    $rows = array();
    while($row = $result->fetch_row()) {
                $rows[]=$row;
                printf("
");
    }

    $result->close();
    var_dump($rows);
    printf("
");

    $delete = join("', '",$rows);
    var_dump($delete);
    printf("
");

    //send delete request here
    $query ="DELETE FROM FILES WHERE event_id=$event_id AND file_id NOT IN ('$delete')";
    $result = $sql->query($query);
    //printf("$query: %s
", $query);
    var_dump($query);
    printf("
");     
    if (!$result) {
        var_dump($result);
        printf("
");
        printf("Query failed: %s
", $mysqli->error);
        sendResponse(417, json_encode("Query failed"));

    exit;
    }

LOG:

string(143) "SELECT file_id FROM FILES WHERE event_id=7 AND 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 FROM FILES WHERE event_id=7 AND file_id NOT IN ('Array', 'Array', 'Array', 'Array', 'Array')"

You can see in the log $delete = join("', '",$rows); causes ('Array', 'Array', 'Array', 'Array', 'Array') is not what I want,It should have been something like ('89','90', '91', '92', '93')

How can I make this work?

  • 写回答

2条回答 默认 最新

  • dtbl1231 2013-05-14 17:18
    关注

    Probably it should be $rows[]=$row['file_id']; or $rows[]=$row[0]; (depends on fetch method). However, this would be more workable:

     DELETE FROM FILES WHERE event_id=$event_id AND name NOT IN ('$names')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了