I recently started coding a script meant for html output that (hopefully) will reduce some busy work at my job. It is a script adapted from a functional script I wrote in python, but I do not think that is the issue.
I have the following code at the start of the php script to identify errors:
ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE);
I have put the program on a server and attempted to run it. My first problem is that partway through the program, it starts dumping the source code (minus HTML tags) to the window partway through an if statement as such:
$moonSet[0]){
$t = 3;
} # Case 4, set time greater than rise time
else {
$t = 4;
}
For reference, the actual section of code interrupted is as such:
# Case 1, no rise time on current date
if ($moonRise[0] == 0){
$t = 1;
} # Case 2, no set time on current/next date
elseif ($moonSet[0] == 0){
$t = 2;
} # Case 3, rise time greater than set time
elseif ($moonRise[0] > $moonSet[0]){
$t = 3;
} # Case 4, set time greater than rise time
else {
$t = 4;
}
I believe the version of php used on the server is at least PHP 5.x, as the source code is dumped to the window when the html coding uses <??>
instead of <?php?>
. The problem is that when I use <?php?>
all the output vanishes--the HTML tags included. Looking at the source code, it only shows a 1 to indicate the first line of code, suggesting that no code is actually in the file. This only happens when I use <?php?>
instead of <??>
.
I then tried to gradually step through the code to identify why, one, the code suddenly interrupts the if statement, and two, why using <?php?>
torches the output.
What I ended up discovering is that the <?php?>
code works just fine until I added in a loop. The program uses a MySQL query, and from what I can tell, the query worked just fine. The program executed, output at both the beginning and end of the test code was displayed to screen.
The SQL code is as such:
$connection = mysql_connect("localhost", "webuser", "webuser");
mysql_select_db("dbAstro", $connection);
$queryTides = "SELECT tideDateTime, tideHeight, tideHorL FROM tblTides WHERE DATE(tideDateTime) BETWEEN '$targetDate[0] 00:00:00' AND '$targetDate[2] 23:59:59' ORDER BY tideDateTime";
$querySun = "SELECT sunDate, sunRise, sunSet FROM tblSunRiseSet WHERE DATE(sunDate) BETWEEN '$targetDate[0] 00:00:00' AND '$targetDate[2] 23:59:59' ORDER BY sunDate";
$queryMoon = "SELECT moonDate, moonRise, moonSet FROM tblMoonRiseSet WHERE DATE(moonDate) BETWEEN '$targetDate[0] 00:00:00' AND '$targetDate[2] 23:59:59' ORDER BY moonDate";
$queryTwi = "SELECT twilightDate, twilightBeg, twilightEnd FROM tblCivilTwilight WHERE DATE(twilightDate) BETWEEN '$targetDate[0] 00:00:00' AND '$targetDate[2] 23:59:59' ORDER BY twilightDate";
#Query results stored in separate result locations for later reference.
$resultTi = mysql_query($queryTides, $connection);
$resultSu = mysql_query($querySun, $connection);
$resultMo = mysql_query($queryMoon, $conneciton);
$resultTwi = mysql_query($queryTwi, $connection);
The difference between using the <?php?>
and <??>
tags so far is that <??>
will not display a printf('') command. <?php?>
displayed the printf('') command, but with a message that a null value is in the database--which there are some null values in the database.
The problem is when the script enters a loop. Upon putting a loop command, either while or for, the <?php?>
script vanishes from both the output and when viewing the source code in a browser. <??>
as a tag still outputs the HTML, but does not output any of the php commands--a basic printf('') or an echo command.
I want to stress that it does this with any loop--not just a while loop that searched the SQL queries (the intended goal, shown abbreviated below):
$b = 0;
while ($astroRow = mysql_fetch_assoc($resultTi)) {
$tideTime[$b] = date_format("$astroRow[tideDateTime]", "hi");
$b++;
}
When I attempted to replace the loop with something extremely basic, like this:
for ($a = 0; $a < 2; $a++) {
echo $a;
}
the same pattern occurred. One would start dumping output to screen or show no output from php, and the <?php?>
tags would show no code or output whatsoever.
I am at a loss. I do not know what the problem is, and attempting to search help sites has not led me to an answer to my problem. Would anyone have an idea what exactly is going on?