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

时间:2016-07-13 13:42:36

标签: php mysql sql

我有一个表,其中包含目录中所有文件的名称。我希望能够从表和目录中删除所有具有特定属性的文件。例如,如果条目没有name,那么我想删除它。我想要使​​用的sql语句是delete from master where name = null,但后来我无法从目录中删除该文件,因为我无法确定要删除哪个文件。在删除它之前,有没有办法将表中条目的值存储在数组中?

1 个答案:

答案 0 :(得分:0)

先做一个选择:

SELECT * FROM master WHERE name = null

然后将结果存储在变量中,您可以使用unlink($fileName)删除文件。

如果你想有效地执行sql速度,请转到

DELETE FROM master WHERE name = null

然而,我个人会循环浏览文件,并按照以下方式单独执行:

    $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
        }
    }

当然你必须用your_sql_select_function和your_sql_delete_function代替你用来处理mysql的函数