0

I am working on a php code as shown below which lists all the mp4 files present in a $src_dir.

$src_dir = ('\\\ABCD-ST-001\Audio_Test\podcast\incoming_folder'); 

$mp4_files = preg_grep('~\.(mp4)$~', scandir($src_dir));

print_r(array_values($mp4_files));  // LineA

Here is the O/P obtained from Line#A:

Array ( [0] => 36031P.mp4 [1] => hello.mp4 )

Now, I have used the following script in my php code in order to insert those mp4 files in Podcast_Export table.

    foreach ($mp4_files as $value) {
        $db->exec(SELECT COUNT(*) FROM Podcast_Export WHERE House# = '".$value."' AND status = 'GO');
        $db->exec("INSERT INTO Podcast_Export ('House_number', 'Status') VALUES ('".$value."', 'Go')");    // Line B
    }

The above script add the following data inside Podcast_Export table:

36031.mp4 Go
hello.mp4 Go

Problem Statement:

The issue which I am facing right now is when I refresh the page, the script at LineB is run again and mp4 files present in a $src_dir is added again inside Podcast_Export table as shown below:

36031.mp4 Go
hello.mp4 Go
36031.mp4 Go
hello.mp4 Go

It should work in a way that once new file comes up inside $src_dir then it should add inside Podcast_Export table. Let us suppose the new file is hxz210.mp4 then the content inside Podcast_Export table should be:

36031.mp4  Go
hello.mp4  Go
hxz210.mp4 Go
8
  • Modify House# so it is a unique value... or change your script so on a refresh it doesn't run again. Commented Jun 19, 2019 at 17:02
  • Or check the database forst to see if the House_Number already exist Commented Jun 19, 2019 at 17:06
  • @user3783243 House# is not a proper column name so I have modified it to 'House_number'. On refresh the script should only run when new mp4 file show up. Commented Jun 19, 2019 at 17:18
  • @flash: again i recommend to add a query which will be used to check either record exist or not link Commented Jun 19, 2019 at 17:21
  • @devpro I have added in the question. Let me know if it looks good. Commented Jun 19, 2019 at 17:25

1 Answer 1

0

Your selection is fine, but you need to store result into an variable like:

$count = $db->querySingle("SELECT COUNT(*) as count FROM Podcast_Export WHERE House_number = '".$value."'"); 
if($count <= 0){ 
    $db->exec("INSERT INTO Podcast_Export (House_number,Status) 
               VALUES ('".$value."', 'Go')");
}

Using querySingle to get the no of rows in SQLite.

note that, i have chanaged the House# to House_number here and removed AND status = 'GO' clause as all of them having GO status.

Sign up to request clarification or add additional context in comments.

19 Comments

The code is not going after the 1st line. I am wondering what is numRows(); in the code above ?
@flash using numrows to get no of rows maybe syntax is diff for sqlite isn't it?
As soon, I comment this line the page starts loading but when I uncomment it the page doesn't load.
@flash u just need to check either query return result or not maybe u need to use diff. Syntax but logic sams
@flash yes ... ... Right
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.