简体   繁体   English

从表中删除条目并存储已删除条目中的值

[英]Delete entries from a table and store a value from the deleted entries

I have a table set up with the names of all the files in a directory. 我有一个表,其中包含目录中所有文件的名称。 I want to be able to delete all the files that have a specific property from both the table and the directory. 我希望能够从表和目录中删除所有具有特定属性的文件。 For example, if the entry does not have a name then I want to delete it. 例如,如果条目没有name那么我要删除它。 The sql statement I thought about using would be delete from master where name = null but then I wouldn't be able to delete the file from the directory because I would have no way to determine which file to delete. 我考虑过使用的sql语句delete from master where name = null但是由于无法确定要删除的文件,因此我无法从目录中删除该文件。 Is there a way to store the values from the entries in the table in an array before I delete them? 在删除它们之前,是否可以将表中条目的值存储在数组中?

Do a select first: 首先进行选择:

SELECT * FROM master WHERE name = null

Then store the results in a variable and you can delete the files with unlink($fileName) . 然后将结果存储在变量中,然后可以使用unlink($ fileName)删除文件。

If you want to do it sql speed efficiently, go for your 如果您想高效地执行sql speed,请使用

DELETE FROM master WHERE name = null

however, I personally would loop through the files and do it individually like this: 但是,我个人将遍历文件并像这样单独进行操作:

    $fileNames = your_sql_select_function("SELECT filename FROM master WHERE name = null");
    foreach($fileNames as $fileName) {
        if(unlink($fileName)) {
            your_sql_delete_function("DELETE FROM master WHERE filename = '$fileName'");            
        }
        else {
            // error report, do not delete it from database yet
            // if you delete it, you won't be able to get the name later 
            // and the file will just hang in there until you 
            // handle it some other way
        }
    }

Of course you have to substitute your_sql_select_function and your_sql_delete_function for the functions you would use to handle mysql 当然,您必须用your_sql_select_function和your_sql_delete_function代替用于处理mysql的函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM