0

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

<?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]." ";
}
}
}
?>

- - - - - - - 编辑 - - - - - - -

我将代码更新为但它决定删除我的整个数据库而不是删除丢失的游戏条目。这是修改后的代码:

    <?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." | ";
  }
}
}
?>

- - - - - -编辑 - - - - - - -

我调试了程序,所以它现在可以工作了。必须在程序中添加一个“ $_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

4

1 回答 1

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 . "'");
  }
}

那应该使它起作用。

于 2013-02-10T23:16:02.003 回答