MySQL结果检查关闭......什么是最好的方法?

时间:2012-04-01 15:05:14

标签: php mysql

我一直在进行递归目录扫描,它扫描文件夹并将它们添加到我的mysql数据库(如果它还没有)。我遇到的唯一问题是无论我怎么写我的表单的Check部分它仍然跳过一些记录和/或当有一些同名的以0结尾的东西时它检测到它已经存在。 +下面的代码。

表:

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.

什么是更精确的检查方法?

2 个答案:

答案 0 :(得分:1)

由于此查询,这是正常的

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

%运算符告诉它接受任何char。在您的规则中,如果左侧值以“绿色唤醒1”开头并以任何字符(甚至不再是字符)结束,则测试将成功

答案 1 :(得分: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');
相关问题