从特定表中选择所有并处理foreach行

时间:2014-01-02 12:53:21

标签: php

我在这里写了一些代码E.G:

数据库表:

IMG_ID|IMG_SRC|EXPIRES|ACTION
------------------------------
 4    | st.jpg |12546564| temp

所以我尝试从DB和目录路径中删除图像到目前为止我得到了这个:

function removeTempPic(){       
    $uploadsDirectory1 = $_SERVER['DOCUMENT_ROOT'].'test/uploads/temp/';  
    //remove after 10 minute if unused
    $timeout = time()-TEMPIC_TIMEOUT*60;
    $q = "SELECT * FROM picsmanager WHERE expires < $timeout AND action=temp";
    mysql_query($q, $this->connection);

    foreach ($row = mysql_fetch_array($q)) {
        $q = "delete * from picsmanager where=??"
       //@unlink($uploadsDirectory1.$uploadFilename);
    }
}

所以我尝试做的是从数据库表中选择all,并在某些行中超时到期,从db和目录中删除每个图像 但这不会起作用,因为我不知道如何正确使用它,谢谢。

2 个答案:

答案 0 :(得分:0)

 function removeTempPic(){

  $uploadsDirectory1 = $_SERVER['DOCUMENT_ROOT'].'test/uploads/temp/';  

  //remove after 10 minute if unused
  $timeout = time()-TEMPIC_TIMEOUT*60;
  $q = "SELECT * FROM picsmanager WHERE expires < $timeout AND action='temp'";
  mysql_query($q, $this->connection);

   foreach($row = mysql_fetch_array($q)){
       $q = sprintf("delete * from picsmanager where IMG_ID = %d", (int)$row['IMG_ID']);
//@unlink($uploadsDirectory1.$uploadFilename);
   }

}

答案 1 :(得分:0)

您在PHP和SQL中有多个错误

// enclose the string "temp" in single quotes
$q = "SELECT * FROM picsmanager WHERE expires < $timeout AND action='temp'";
$result = mysql_query($q, $this->connection);
// you need the result, not $q again
while($row = mysql_fetch_array($result)){
   // run a new query to delete the extracted image
   mysql_query("delete from picsmanager where IMG_ID=" . $row['IMG_ID'] . " LIMIT 1", $this->connection);
   // delete the corresponding file
   unlink($uploadsDirectory1.$row['IMG_SRC']);
}

此外,不再使用mysql_*函数,它们将被弃用,将来将从PHP中删除。您的代码将停止工作。了解如何使用mysqli_*或PDO对象。