2012-04-01 132 views
0

我一直在做遞歸目錄掃描掃描文件夾,並將它們添加到我的mysql數據庫,如果它不在那裏。我唯一的問題是,無論我如何編寫表單的Check部分,它仍然跳過一些記錄和/或它檢測到它已經在那裏,當有一些相同的名稱以0結尾時。 +以下代碼。MySQL結果檢查關閉...什麼是最佳方法?

表:

CREATE TABLE IF NOT EXISTS `comics` (
    `id` int(255) NOT NULL AUTO_INCREMENT, 
    `publisher` varchar(255) NOT NULL, 
    `arc` varchar(255) NOT NULL, 
    `issue` int(5) NOT NULL, 
    `title` varchar(255) NOT NULL, 
    `plot` longtext NOT NULL, 
    `price` varchar(20) NOT NULL DEFAULT '0', 
    `files` int(255) NOT NULL, 
    `size` varchar(255) NOT NULL, 
    `cover` text NOT NULL, 
    `month` int(2) unsigned zerofill NOT NULL, 
    `day` int(2) unsigned zerofill NOT NULL, 
    `year` int(4) unsigned zerofill NOT NULL, 
    `added` date NOT NULL, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `arc` (`arc`,`issue`,`title`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; 

記錄:

INSERT INTO `comics` (`id`, `publisher`, `arc`) VALUES 
(10463, '', 'Green Wake 10'); 
INSERT INTO `comics` (`id`, `publisher`, `arc`) VALUES 
(10462, '', 'Green Wake 1'); 

如果第一插入件是第二插入之前存在,則第二個回來爲已經是在數據庫中。

代碼,它位於現在:

<?php 
$base = "D:/comics/"; 
if (isset($_REQUEST['sub'])) { 
$sub = $_REQUEST['sub']; 
echo "<h2>Scanning Comics Starting With ".$sub."</h2>"; 
    if ($handle = opendir('D:/comics/'.$sub.'/')) { 

     while (false !== ($entry = readdir($handle))) { 
      if ($entry != '$RECYCLE.BIN' && $entry != 'System Volume Information' && $entry != '.' && $entry != '..') { 
      $entry1 = $entry; 
      $entry = str_replace(" - ", ": ", $entry); 
      $exists = mysql_query('SELECT * FROM comics WHERE arc LIKE "'.$entry.'%"'); 
      $exists1 = mysql_num_rows($exists); 
       if ($exists1 == 0) { 
       $directory = $base.$sub."/".$entry1; 
       $filecount = count(glob($directory."/"."*.*")); 
       $size2 = 0; 
       $d = new RecursiveIteratorIterator( 
       new RecursiveDirectoryIterator($directory), 
       RecursiveIteratorIterator::SELF_FIRST 
       ); 

       foreach($d as $file){ 
       $size2 += $file->getSize(); 
       } 
       $size = $size2/1024; 
       $modified = date("Y-m-d", filemtime($directory)); 

        mysql_query('INSERT INTO comics (arc, files, size, added) VALUES ("'.$entry.'", "'.$filecount.'", "'.$size.'", "'.$modified.'")'); 
        print "<li>Inserted <b>".$entry."</b> With A File Count Of <b>".$filecount."</b> & A Total Size Of <b>".$size."</b> MB Last Modified On <b>".$modified."</b>.</li>"; 
       } 
      } 
     } 
    } 
}  
    ?> 

不同的支票查詢我想:

$exists = mysql_query('SELECT * FROM comics WHERE arc LIKE "'.$entry.'%"'); 
// Returns As Already Being In The Database. 

$exists = mysql_query('SELECT * FROM comics WHERE arc = "'.$entry.'"'); 
// Returns The Successfully Imported Message (but doesn't execute the insert query) For All Folders Found. Even If It Is There Or Not. 

這將是一個更精確的檢查方法?

回答

1

這是正常的,因爲這個查詢

$exists = mysql_query('SELECT * FROM comics WHERE arc LIKE "'.$entry.'%"'); 
'Green Wake 10' like 'Green Wake 1%' will return true 

的%運算符告訴它接受任何字符。在你的規則中,如果左邊的值以'Green Wake 1'開始並且以任何字符結束(甚至不再有字符),測試將會成功。

+0

是的,我認爲這就是爲什麼我試圖匹配它'arc ='$ entry''但是也不能正確返回。 – rackemup420 2012-04-01 15:15:19

+0

究竟是什麼返回'$ exists = mysql_query('SELECT * FROM comics WHERE arc =''。$ entry。'「');'和'exists1 = mysql_num_rows($ exists); 'set exists1 = 0? – grifos 2012-04-01 15:28:36

+0

此外,我還建議您在附加到您的查詢的文本上使用'mysql_real_escape_string',以避免任何錯誤(減少sqli漏洞,仍然容易發生評論char)。然而,對於給定的測試值,它不應該改變任何東西。 – grifos 2012-04-01 15:30:50

0

請不要引用'中的數值。您的專欄id的類型爲INT

所以

INSERT INTO `comics` (`id`, `publisher`, `arc`) VALUES 
('10463', '', 'Green Wake 10'); 
INSERT INTO `comics` (`id`, `publisher`, `arc`) VALUES 
('10462', '', 'Green Wake 1'); 

應該

INSERT INTO `comics` (`id`, `publisher`, `arc`) VALUES 
(10463, '', 'Green Wake 10'); 
INSERT INTO `comics` (`id`, `publisher`, `arc`) VALUES 
('0462, '', 'Green Wake 1'); 
+0

是的,當我在這裏鍵入它時意外地添加了它們,現在,因爲他們甚至在我真正的代碼抱歉回合。 – rackemup420 2012-04-01 15:09:56

相關問題