php runs different from command line?
Posted by: MajorGeek
Posted on: 2007-02-01 16:29:00
separated my project into a php script that screen scrapes the data from a BOR website and a script that plots the data. I thought that I would run the screen scraping script early in the morning as a cron, but it won't run to incompletion and insert rows into my MySQL table when run from a command line. Yes, I chmod +x 'ed it. But the script runs fine when called from a browser. I don't think I can figure out how to get windows to run the browser request as a scheduled job.
If I run this from a browser, it prints out the diagnostic lines if uncommented and inserts the rows into the MySQL table. If I run it from a command line, it will print out line 21, 33, but doesn't get to line 60,72, and 73 and no lines are inserted in the table. From a command line, I can get to line 38, but not to line 41. Is it something about creating a new database handle? All I get is:
[kiki]$ ./getFlow4.php
X-Powered-By: PHP/4.4.4
Content-type: text/html
12 $ch = curl_init();
13 $timeout = 5; // set to zero for no timeout
14 curl_setopt ($ch, CURLOPT_URL, $theurl);
15 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
16 curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
17 $contents = curl_exec($ch);
18 curl_close($ch);
19
20 // display file
21 //echo $contents;
22
23 // The feed appears to have useful parsing point identifiers.
24 // I just used them to get everything between them (including flags)
25 $cStartStr = "BEGIN DATA";
26 $cEndStr = "END DATA";
27 $cPageTail = stristr($contents, $cStartStr);
28 $nUsefulDataEndPos = strpos($cPageTail, $cEndStr);
29 $cUsefulData = substr($cPageTail, 0, $nUsefulDataEndPos);
30
31 // explode the content using newlines as delimeters
32 $aContents = explode(chr(10), $cUsefulData);
33 //print_r($aContents);
34
35 // Two new arrays initialized to empty
36 //$myDates = array();
37 //$myTemps = array();
38
39 // Set up and test database connection
40 @ $db = new mysqli('bor.ykfp.org', 'xxxxx', 'xxxxxxx', 'xxx');
41
42 if (mysqli_connect_errno())
43 {
44 echo 'Error: Could not connect to database. Please try again later .';
45 exit;
46 }
47
48 // Empty the temperature table in preparation for filling it up with rev ised data
49 $query = "DELETE from Flow";
50 $result = $db->query($query);
51
52 // skip the leading and trailing junk
53 // Prolly don't want to do all these assignments in the loop. They're ju st used for readability
54 for ($i=3; $i<count($aContents)-1; $i++) {
55
56 // Dates are formatted as 10 characters
57 $cDateStr = substr($aContents[$i],0,10);
58 #convert date string (a date in human language) to a date in programming language
59 $myDates = strtotime($cDateStr);
60 echo $cDateStr, $myDates;
61
62 // QD is everything in the trimmed value after the last space
63 $nQDVal = substr($aContents[$i], strrpos(trim($aContents[$i]), c hr(32))+1);
64 //$query = "insert into Flow values ('".$myDates."', '".$nQDVal. "', '".YRPW."')";
65 $nQDVal == "MISSING " ?
66 $query = "insert into Flow values ('".$myDates."', null, '".YRPW."')" :
67 $query = "insert into Flow values ('".$myDates."', '".$n QDVal."', '".YRPW."')";
68 $result = $db->query($query);
69 }
70 //peep scene
71 //echo('<pre>');
72 //print_r($nQDVal);
73 //print_r($myDates);
74 //echo('</pre>');
75
This signature line intentionally blank.