AV1611 Posted January 1, 2007 Share Posted January 1, 2007 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 More sharing options...
corbin Posted January 1, 2007 Share Posted January 1, 2007 [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 More sharing options...
AV1611 Posted January 1, 2007 Author Share Posted January 1, 2007 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 More sharing options...
AV1611 Posted January 1, 2007 Author Share Posted January 1, 2007 //bump I edited my post... Link to comment https://forums.phpfreaks.com/topic/32489-rerun-script-on-error/#findComment-150990 Share on other sites More sharing options...
JasonLewis Posted January 1, 2007 Share Posted January 1, 2007 i dont no if you can do headers is php commandline but this is how they look:[code=php:0]header("Location: php /path/to/myscript.php");[/code] Link to comment https://forums.phpfreaks.com/topic/32489-rerun-script-on-error/#findComment-150995 Share on other sites More sharing options...
corbin Posted January 1, 2007 Share Posted January 1, 2007 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 More sharing options...
AV1611 Posted January 2, 2007 Author Share Posted January 2, 2007 No I'm parsing a text file into a table. sometimes the file connection doesn't work. that is when I get the blank data.I just want to rerun the script if it fails Link to comment https://forums.phpfreaks.com/topic/32489-rerun-script-on-error/#findComment-151146 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.