Wallboard System PHP Parse error

I am re-building a wallboard system for a restaurant in my area. And this time around we are dealing with a lot more moving parts. With that being said, I have managed to get everything to work on the backend, but for some reason when I am trying to call the data to display on the front end and render the page, I get a 500 error. When I look at the logs, this is what I get:
PHP Parse error: syntax error, unexpected end of file in /var/www/html/index.php on line 718

Now there is a lot more code than this, but this is where it is saying the problem is in the log:

// Retrieve Monthly Dinner Car Count Record Data

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {
            echo "Error: If problem persists please contact [email protected] - 6100\n";
            $content = "Error:  . $conn->connect_error  . -6100\n";
            file_put_contents('main_log.txt', $content);
}

$sql = "SELECT date_cc, team_cc, car_count FROM monthly_dinner_cc";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
	$mdated_cc = $row["date_cc"];
	$mteamd_cc = $row["team_cc"];
	$mccountd = $row["car_count"];
}
}
else {
	echo "Error Retrieving Data/No Data Found in monthly dinner table, if problem persists please contact [email protected]\n";
}
$conn->close();
    
?>

Any suggestions as to a better approach to this/why it isn’t working?

Thank you in advance!

The posted code does not produce a php parse/syntax error. You would need to post more of the actual code on the page.

The error you are getting means php was still expecting some php code when it reached the end of the file. This is generally caused by mis-matched quotes or mis-matched braces {}.

If you are making a database connection near line 700 in a file, your code is not organized very well and/or you are making a new database connection just to run a block of code, which will result in multiple connections and a slow page. You should make one database connection on a page and the database specific code should come before the start of the html document. There should be a minimum of simple conditional logic/loops/echos in the actual html document.

If you use exceptions to handle database statement errors, you can eliminate all the conditional logic in your code (except when handling the insert/update of duplicate data.) To output a custom user error message, you would add an exception handler of your own. You should also append content to your error log file and include a date/time stamp to each logged error so that you known when a problem started/occurred.

Here is the rest of my code:

<?php
require __DIR__ . '/include/credentials.php';



// Retrieve Morning Dollar Amount Record Data

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {
            echo "Error: If problem persists please contact [email protected] - 5000\n";
            $content = "Error:  . $conn->connect_error  . -5000\n";
            file_put_contents('main_log.txt', $content);
$sql = "SELECT date_da, team_da, dollar_amount FROM morning_da";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $dateb_da = $row["date_da"];
	    $teamb_da = $row["team_da"];
	    $damountb = $row["dollar_amount"];
    }
}
else {
    echo "Error Retrieving Data/No Data Found in morning dollar amount table, if problem persists please contact [email protected]\n";
}
$conn->close();

// Retrieve Morning Car Count Record Data

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {
            echo "Error: If problem persists please contact [email protected] - 5100\n";
            $content = "Error:  . $conn->connect_error  . -5100\n";
            file_put_contents('main_log.txt', $content);
$sql = "SELECT date_cc, team_cc, car_count FROM morning_cc";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $dateb_cc = $row["date_cc"];
	    $teamb_cc = $row["team_cc"];
	    $ccountb = $row["car_count"];
    }
}
else {
    echo "Error Retrieving Data/No Data Found in morning dollar amount table, if problem persists please contact [email protected]\n";
}
$conn->close();

// Retrieve Lunch Dollar Amount Record Data

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {
            echo "Error: If problem persists please contact [email protected] - 5200\n";
            $content = "Error:  . $conn->connect_error  . -5200\n";
            file_put_contents('main_log.txt', $content);
$sql = "SELECT date_da, team_da, dollar_amount FROM lunch_da";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
	   $datel_da = $row["date_da"];
	   $teaml_da = $row["team_da"];
	   $damountl = $row["dollar_amount"];
}
}
else {
	echo "Error Retrieving Data/No Data Found in lunch table, if problem persists please contact [email protected]\n";
}
$conn->close();
    
// Retrieve Lunch Car Count Record Data

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {
            echo "Error: If problem persists please contact [email protected] - 5300\n";
            $content = "Error:  . $conn->connect_error  . -5300\n";
            file_put_contents('main_log.txt', $content);
$sql = "SELECT date_cc, team_cc, car_count FROM lunch_cc";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
	   $datel_cc = $row["date_cc"];
	   $teaml_cc = $row["team_cc"];
	   $ccountl = $row["car_count"];
}
}
else {
	echo "Error Retrieving Data/No Data Found in lunch table, if problem persists please contact [email protected]\n";
}
$conn->close();

// Retrieve Dinner Dollar Amount Record Data

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {
            echo "Error: If problem persists please contact [email protected] - 5400\n";
            $content = "Error:  . $conn->connect_error  . -5400\n";
            file_put_contents('main_log.txt', $content);
$sql = "SELECT date_da, team_da, dollar_amount FROM dinner_da";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
	$dated_da = $row["date_da"];
	$teamd_da = $row["team_da"];
	$damountd = $row["dollar_amount"];
}
}
else {
	echo "Error Retrieving Data/No Data Found in dinner table, if problem persists please contact [email protected]\n";
}
$conn->close();
    
// Retrieve Dinner Car Count Record Data

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {
            echo "Error: If problem persists please contact [email protected] - 5500\n";
            $content = "Error:  . $conn->connect_error  . -5500\n";
            file_put_contents('main_log.txt', $content);
$sql = "SELECT date_cc, team_cc, car_count FROM dinner_cc";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
	$dated_cc = $row["date_cc"];
	$teamd_cc = $row["team_cc"];
	$ccountd = $row["car_count"];
}
}
else {
	echo "Error Retrieving Data/No Data Found in dinner table, if problem persists please contact [email protected]\n";
}
$conn->close();

// Retrieve Monthly Morning Dollar Amount Record Data

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {
            echo "Error: If problem persists please contact [email protected] - 5600\n";
            $content = "Error:  . $conn->connect_error  . -5600\n";
            file_put_contents('main_log.txt', $content);
$sql = "SELECT date_da, team_da, dollar_amount FROM monthly_morning_da";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $mdateb_da = $row["date_da"];
	    $mteamb_da = $row["team_da"];
	    $mdamountb = $row["dollar_amount"];
    }
}
else {
    echo "Error Retrieving Data/No Data Found in monthly morning table, if problem persists please contact [email protected]\n";
}
$conn->close();
    
// Retrieve Monthly Morning Car Count Record Data

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {
            echo "Error: If problem persists please contact [email protected] - 5700\n";
            $content = "Error:  . $conn->connect_error  . -5700\n";
            file_put_contents('main_log.txt', $content);
$sql = "SELECT date_cc, team_cc, car_count FROM monthly_morning_cc";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $mdateb_cc = $row["date_cc"];
	    $mteamb_cc = $row["team_cc"];
	    $mccountb = $row["car_count"];
    }
}
else {
    echo "Error Retrieving Data/No Data Found in monthly morning table, if problem persists please contact [email protected]\n";
}
$conn->close();

// Retrieve Monthly Lunch Dollar Amount Record Data

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {
            echo "Error: If problem persists please contact [email protected] - 5800\n";
            $content = "Error:  . $conn->connect_error  . -5800\n";
            file_put_contents('main_log.txt', $content);
$sql = "SELECT date_da, team_da, dollar_amount FROM monthly_lunch_da";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
	   $mdatel_da = $row["date_da"];
	   $mteaml_da = $row["team_da"];
	   $mdamountl = $row["dollar_amount"];
}
}
else {
	echo "Error Retrieving Data/No Data Found in monthly lunch table, if problem persists please contact [email protected]\n";
}
$conn->close();
    
// Retrieve Monthly Lunch Car Count Record Data

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {
            echo "Error: If problem persists please contact [email protected] - 5900\n";
            $content = "Error:  . $conn->connect_error  . -5900\n";
            file_put_contents('main_log.txt', $content);
$sql = "SELECT date_cc, team_cc, car_count FROM monthly_lunch_cc";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
	   $mdatel_da = $row["date_cc"];
	   $mteaml_da = $row["team_cc"];
	   $mccountl = $row["car_count"];
}
}
else {
	echo "Error Retrieving Data/No Data Found in monthly lunch table, if problem persists please contact [email protected]\n";
}
$conn->close();

// Retrieve Monthly Dinner Dollar Amount Record Data

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {
            echo "Error: If problem persists please contact [email protected] - 6000\n";
            $content = "Error:  . $conn->connect_error  . -6000\n";
            file_put_contents('main_log.txt', $content);
}

$sql = "SELECT date_da, team_da, dollar_amount FROM monthly_dinner_da";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
	$mdated_da = $row["date_da"];
	$mteamd_da = $row["team_da"];
	$mdamountd = $row["dollar_amount"];
}
}
else {
	echo "Error Retrieving Data/No Data Found in monthly dinner table, if problem persists please contact [email protected]\n";
}
$conn->close();
    
// Retrieve Monthly Dinner Car Count Record Data

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {
            echo "Error: If problem persists please contact [email protected] - 6100\n";
            $content = "Error:  . $conn->connect_error  . -6100\n";
            file_put_contents('main_log.txt', $content);
}

$sql = "SELECT date_cc, team_cc, car_count FROM monthly_dinner_cc";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
	$mdated_cc = $row["date_cc"];
	$mteamd_cc = $row["team_cc"];
	$mccountd = $row["car_count"];
}
}
else {
	echo "Error Retrieving Data/No Data Found in monthly dinner table, if problem persists please contact [email protected]\n";
}
$conn->close();
    
?>

I am sure there is a better way to do this, but this is my first project, so I am learning as I am building, error logging is my next thing I am redoing.

Also, even though the error says it’s on line 700, my PHP only goes to line 300. When I tested just the PHP it gave me the same error just at the new end. I’m guessing that I am missing something big, but I honestly don’t know what I am looking for.

LOL. STOP. In addition to the previously stated issues, you are using a while() loop to fetch a single row of data and your database design is bad.

In short, you have created a needless wall of code and cannot find what is probably a simple mistake (I cannot see it either and am not going to waste time looking at it now.)

Your code should -

  1. Make one database connection and use that connection throughout the code.
  2. If you are only fetching a single row of data from a query, don’t use a loop. Just call the fetch statement. I somehow suspect you are actually trying to fetch the last row from each query. You should instead query for only the last row, by using an ORDER BY … DESC LIMIT 1.
  3. Don’t copy variables to other variables without any purpose. For what you have shown, just fetch the data into an appropriately named array variable, then use elements of that variable in the html document.

Your database apparently has several tables, each holding a single row that you are UPDATEing (accumulating) values in. You should instead insert a new row for each data item, then use an appropriate sql query to get the correct result.

1 Like

Okay, I see the problem. It’s due to mis-matched braces {} that are part of the repetitive connection error handling. The last two sets, one of which you posted at the top of this thread, are correct. All the others are not. If you eliminate all but one set of the connection/connection error handling code, the problem should go away.

1 Like

Continuation of the above list -
4. If you just execute the fetch statement, in an if() conditional test, you can also eliminate all the num_rows logic.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service