2013-02-10 38 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

相關問題