Jump to content

rerun script on error


AV1611

Recommended Posts

ok,

I'm parsing out a text file.

sometimes, the file is empty and the data is missing at the 3rd array offset.

How do I make the whole script restart if the 3rd element is no set?

I would like it to just reload the script page.

Oh, and by the way, this is a CRON file that I fire off like this: 
php /path/to/file/script.php &

[code]
<?php
$user="xxxx";
$host="192.168.xxx.xxx";
$password="xxxx";
$database = "xxxx";
mysql_connect($host,$user,$password) or die('croaked');
mysql_select_db($database);
$sql=mysql_query("delete from mbl_copy");
$lines = file('http://xxxxxxxx/textfile.txt');
foreach ($lines as $line_num => $line) {
  if($line_num > 1){
        $data=explode(" ",$line);
        $aa=$data[0]." ".$data[1];
        $bb=$data[2];
        $cc=$data[3]; //<------this is the line that first shows up blank if the file is empty. 
        $dd=$data[5];
        $ee=trim($data[6]);
        $sql=mysql_query("insert into mbl_copy values ('$aa','$bb','$cc','$dd','$ee')");
        }
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/32489-rerun-script-on-error/
Share on other sites

[code=php:0]
<?php
$user="xxxx";
$host="192.168.xxx.xxx";
$password="xxxx";
$database = "xxxx";
mysql_connect($host,$user,$password) or die('croaked');
mysql_select_db($database);
$sql=mysql_query("delete from mbl_copy");
$lines = file('http://xxxxxxxx/textfile.txt');

$count = count($lines);
while($i < ($lines - 1)) {

        $data=explode(" ",$lines[$i]);
        $aa=$data[0]." ".$data[1];
        $bb=$data[2];
        $cc=$data[3]; //<------this is the line that first shows up blank if the file is empty. 
        $dd=$data[5];
        $ee=trim($data[6]);
if(isset($cc)) {
$i++;
}
else {
$i2++;
}

if($i2 >= 10) {
die("The lines still empty!");
}
        $sql=mysql_query("insert into mbl_copy values ('$aa','$bb','$cc','$dd','$ee')");
}
?>
[/code]

I hope that does what you want... The only bad thing is, the remote file is being updated periodically right?  So theoretically this script could fail after it tried 10 times, and then the DB would be empty... If the script never had a catch its self point though, it could freeze apache or IIS or w/e lol
Link to comment
https://forums.phpfreaks.com/topic/32489-rerun-script-on-error/#findComment-150960
Share on other sites

I think I still get the error msg if the [2] is blank...
here is what I thought up after I posted...
think this will work?  the file is only updated every 6 hours...
[code]
$lines = file('http://members.dslextreme.com/users/aon/pbcool.txt');
foreach ($lines as $line_num => $line) {
  if($line_num > 1){
        $data=explode(" ",$line);
        $aa=$data[0]." ".$data[1];
        $bb=$data[2];
if($line_num == 2 && isset($data[3])){
        $cc=$data[3];
$sql=mysql_query("delete from mbl_copy");
}
elseif($line_num >= 3) {
$cc=$data[3];
}
else{
die('MBL SCRIPT SCRIPTNAME.PHP FAILED TO IMPORT FILE');
}
        $dd=$data[5];
        $ee=trim($data[6]);
        $sql=mysql_query("insert into mbl_copy values ('$aa','$bb','$cc','$dd','$ee')");
        }
}
[/code]

My thought is replace the die() with header() but does anyone know if you can do header() with a php commandline script?

header(location: 'php /path/to/myscript.php')
is that valid?
Link to comment
https://forums.phpfreaks.com/topic/32489-rerun-script-on-error/#findComment-150974
Share on other sites

Hmmm so I'm guessing that text file is usernames and passwords...

And I'm guessing the problem is, its inserting a blank row which is allowing people to log in with out a username or pass...

So couldnt you just not insert the row if any part of it was blank?  Well, assuming this is the case...
Link to comment
https://forums.phpfreaks.com/topic/32489-rerun-script-on-error/#findComment-151011
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.