フォルダをスキャンしてmysqlデータベースに追加している再帰的なディレクトリスキャンに取り組んでいます。私が持っている唯一の問題は、フォームのチェック部分をどのように書いているように見えても、いくつかのレコードをスキップしたり、同じ名前のものが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');
第二のインサートが、その後、二番目が既にデータベースにあるものとして戻ってくる前に、最初の挿入が存在する場合。それが今座ったよう
コード:
$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.
をより精密な検査方法がどうなるか:私が試した
<?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>";
}
}
}
}
}
?>
異なるチェッククエリ?
ええ、なぜ私はそれが 'arc = '$ entry''と一致するようにしようとしたのかということを考えましたが、それは正しいものでもありません。 – rackemup420
'$ exists = mysql_query( 'SELECT * FROM com = WHERE arc ="'。$ entry。 '"');'と 'exists1 = mysql_num_rows($ exists);を正確に返します。 'set exists1 = 0? – grifos
また、クエリに追加されたテキストに 'mysql_real_escape_string'を使ってバグを避けることをお勧めします。しかし、与えられたテスト値では何も変えてはいけません。 – grifos