S
S
Sergey2018-02-06 13:42:13
PHP
Sergey, 2018-02-06 13:42:13

Checking an existing record in mysql using php?

The task is to scan the directory with files for * .zip, let's say we got the result

files
  1.zip
  2.zip
  3.zip
  ...
  ...
  183.zip

after we check in mysql whether there is a 1.zip entry in the file_name table, if not, add it if there is, then proceed to checking the next file
Co scanning seems to have figured it out
$dir = "/home/main/files";
$list = scandir($dir);

foreach ($list as $item) 
{
  echo "$item" . PHP_EOL;
}

that's what I got the data is added only there is no check for existing records
$dir = "/home/main/files";
$list = scandir($dir);

foreach ($list as $item) 
{
  $connect->query("INSERT INTO `cloud` (`id`, `file_name`, `date`) VALUES (NULL, '$item', NULL)");
  echo "$item" . PHP_EOL;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Timofeev, 2018-02-06
@webinar

It is better to do a check either with a count request or generally make 1 request, select everything and check in php in_array()so as not to climb into the database every time. But it depends on the number of records in the database.

S
Sergey, 2018-02-06
@Raschen

Or maybe an example, preferably simpler, of how it's done in order to understand the logic of how it all works
Records are small 2000

A
Alexander, 2018-02-06
@AK-VoronM

Good afternoon. If the filename is unique, make it an index. Then it will be possible to use INSERT IGNORE . This construct will only insert a record if the key is unique, otherwise it will do nothing. Link to documentation .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question