嗨,我正试图从嵌入字段中获取文件路径,请使用该路径查看该路径中是否存在文件。如果文件不存在,则删除该条目。我正在运行一个游戏网站,下载游戏文件的脚本跳过了一些,所以这就像在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
我看到一个可能是你的问题的拼写错误:
$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 . "'");
}
}
这应该会让它发挥作用。