2013-02-10 33 views
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]." "; 
} 
} 
} 
?> 

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

我更新的代码BUT它决定删除我的整个数据库,而不是删除缺少的游戏条目。下面是修改后的代码:

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

谢谢所有的帮助特别是加格龙。

对于那些谁不知道这个节目是我的网站: http://www.FlameGame.net

+0

你得到的输出是什么? – Achrome 2013-02-10 22:24:29

+0

什么都没有 - 页面只是重新加载没有输出 – user2030325 2013-02-10 22:31:33

回答

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可以转而通过迭代

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

这应该使它工作。

+0

我实现的代码,我得到一个错误 警告:file_exists()期望参数1是字符串,给出的数组 – user2030325 2013-02-10 23:34:38

+0

@ user2030325哦,对不起,对。 '$ row'是数据库中字段的数组。你可能应该使用$ row ['embed'],现在编辑答案。 – Gargron 2013-02-11 00:09:51

+0

我运行程序 - 没有错误,但它决定删除我的数据库中的所有内容,而不是删除我想要删除的内容。一些小的不便重建数据库。 – user2030325 2013-02-11 00:22:31

相关问题