0

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?

  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Sep 29 '16 at 22:21
  • Are you sure you have not just forgotten to match the `` tags around all PHP code? – RiggsFolly Sep 29 '16 at 22:24
  • The sql system used where I work is mysql, I encountered it when writing the initial script in Python. Yes, that was one of the first things I checked. The brackets match, there is one and one ?> at the end of the PHP code. – tenkiforecast Sep 30 '16 at 01:22
  • A `` only works if a php.ini param is set. Use ` – RiggsFolly Sep 30 '16 at 07:54
  • I've tried using – tenkiforecast Sep 30 '16 at 19:26

0 Answers0