使用文件检查器搜索和删除数据库行

时间:2013-02-10 22:22:25

标签: php mysql database file erase

您好我正在尝试从嵌入字段获取文件路径,使用该路径查看该路径中是否存在文件。如果该文件不存在则删除该条目。我正在运行一个游戏网站,下载游戏文件的脚本跳过了一些,所以就像在4000条目db haystack中查找一个针。任何帮助表示赞赏。这是我的代码:

<?php
if(isset($_POST['clean'])) {
$query = "SELECT * FROM games WHERE embed";
$result = mysql_query($query) or die ("no query");

$result_array_path = array();
while($row = mysql_fetch_assoc($result))
{
    $result_array_path = $row;
}
$count = mysql_num_rows($result);
for($counter=0;$counter<$count;$counter++){
if (file_exists($result_array_path[$counter])){

}else{
    mysql_query("DELETE FROM games WHERE embed".$result_array_path[$counter]);
    echo $result_array_path[$counter]." ";
}
}
}
?>

-------------- EDIT -------------

我更新了代码但是它决定删除我的整个数据库,而不是删除丢失的游戏条目。以下是修订后的代码:

    <?php
if(isset($_POST['clean'])) {
$query = "SELECT * FROM games WHERE embed NOT LIKE '%mochiads.com%'";
$result = mysql_query($query) or die ("no query");

$result_array_path = array();
while($row = mysql_fetch_assoc($result))
{
    $result_array_path[] = $row['embed'];
}
foreach($result_array_path as $path) {
  if(!file_exists($path)) {
    mysql_query("DELETE FROM games WHERE embed = '" . $path . "'");
    echo $path." | ";
  }
}
}
?>

----------- -------------- EDIT

我调试了程序,现在可以正常工作了。不得不在程序中添加“$_SERVER['DOCUMENT_ROOT']”。这是完成的程序

<?php
if(isset($_POST['clean'])) {
$query = "SELECT * FROM games WHERE embed NOT LIKE '%mochiads.com%'";
$result = mysql_query($query) or die ("no query");

$result_array_path = array();
while($row = mysql_fetch_assoc($result))
{
    $result_array_path[] = $row['embed'];
}
 foreach($result_array_path as $path) {
      $rel_path = str_replace('http://www.flamegame.net', '', $path);
      $rel_path = str_replace('http://flamegame.net', '', $rel_path);
      $rel_path = str_replace('%20', ' ', $rel_path);

      if(! file_exists($_SERVER['DOCUMENT_ROOT'] . $rel_path)) {
    mysql_query("DELETE FROM games WHERE embed = '" . $path . "'");
    echo $rel_path." | ";
  }
}
}
?>

感谢所有帮助特别是Gargron。

对于那些想知道这个程序适用于我的网站的人: http://www.FlameGame.net

1 个答案:

答案 0 :(得分:0)

我看到一个错字可能是你的问题:

$result_array_path = array();

while($row = mysql_fetch_assoc($result))
{
  // You want to append the row to the results array
  // instead of replacing the array each time, so []
  $result_array_path[] = $row['embed'];
  // That's assuming the table field containing the path is "embed"
}

而不是使用mysql_num_rows而是可以反复遍历$result_array_path中的项目:

foreach($result_array_path as $path) {
  if(! file_exists($path)) {
    // Delete
    // You missed a = in the query too
    mysql_query("DELETE FROM games WHERE embed = '" . $path . "'");
  }
}

这应该有效。