Issues with php file throwing 500 ERROR while in apache test environment


#1

I built a basic application that is pulling data from an outside source and storing it in the database.

My next step is retrieving this information and using it in the HTML output for the dashboard. The code I came up with is below, and it is the most basic query and variable system I can think of, yet when I put it into my test environment, it doesn’t work. I would consider myself relatively new to php in this application, but from all the research I have done, this seems to be the best route. Any help or suggestions would be appreciated.

I have tried several different styles for making a sql connection, and even tried storing the variables globally, but had no luck. Also, I have put this code through several formatters to ensure everything is correct, as well as going over by hand after to try and make sure it didn’t add something it shouldn’t have.

<?php
$servername = "localhost";
$username = "root";
$password = "******";
$dbname = "wallboard";

// Retreave Morning Record Data

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

// Check connection

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT date, team, dollar_amount, car_count FROM morning";

if ($result = mysqli_query($conn, $sql)) {

    // output data into variables

    $dateb = $result["date"];
    $teamb = $result["team"];
    $damountb = $result["dollar_ammount"];
    $ccountb = $result["car_count"];
}
else {
    echo "Error Retreaving Data/No Data Found in morning table";
}

$conn->close();

// Retreave Lunch Record Data

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

// Check connection

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT date, team, dollar_amount, car_count FROM lunch";

if ($result = mysqli_query($conn, $sql)) {

    // output data into variables

    $datel = $result["date"];
    $teaml = $result["team"];
    $damountl = $result["dollar_ammount"];
    $ccountl = $result["car_count"];
}
else {
    echo "Error Retreaving Data/No Data Found in lunch table";
}

$conn->close();

// Retreave Lunch Record Data

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

// Check connection

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT date, team, dollar_amount, car_count FROM dinner";

if ($result = mysqli_query($conn, $sql)) {

    // output data into variables

    $dated = $result["date"];
    $teamd = $result["team"];
    $damountd = $result["dollar_ammount"];
    $ccountd = $result["car_count"];
}
else {
    echo "Error Retreaving Data/No Data Found in dinner table";
}

$conn->close();
?>
<!DOCTYPE html>
<html>
<head>
    <div>
    <div>
    <h1>Drive Thru Records</h1>
    </div>
    <div>
    <h2>Breakfast Record</h2>
        <h3>Team</h3>
        <p><?php
echo $teamb;
?></p>
        <h3>Dollar Amount</h3>
        <p><?php
echo $damountb;
?></p>
        <h3>Car Count</h3>
        <p><?php
echo $ccountb;
?></p>
        <h3>Date</h3>
        <p><?php
echo $dateb;
?></p>
    </div>
        <div>
    <h2>Lunch Record</h2>
        <h3>Team</h3>
        <p><?php
echo $teaml;
?></p>
        <h3>Dollar Amount</h3>
        <p><?php
echo $damountl;
?></p>
        <h3>Car Count</h3>
        <p><?php
echo $ccountl;
?></p>
            <h3>Date</h3>
        <p><?php
echo $datel;
?></p>
    </div>
        <div>
    <h2>Dinner Record</h2>
        <h3>Team</h3>
        <p><?php
echo $teamd;
?></p>
        <h3>Dollar Amount</h3>
        <p>?php echo $damountd; ?></p>
        <h3>Car Count</h3>
        <p><?php
echo $ccountd;
?></p>
            <h3>Date</h3>
        <p><?php
echo $dated;
?></p>
    </div>
    </div>
    </head></html>

Expected result would be that the program pulls the data from the database, assign it to the variables, and then uses echo to place in the correct spot in the HTML.

At this point when I run the code in my test environment, all I get is a page of the code, and no logs or anything to help on screen, I’m stuck, please help!


#2

This typically translates to one of two things. Either your test environment isn’t running php, or you aren’t accessing the page correctly through the server.


#3

@astonecipher The server has PHP installed, and everything runs fine with another application on the same server/directory. As far as accessing the page goes, I have apache2 running to serve the application into the browser, but I am still only left with the code readout. Any other suggestions as to what might be causeing this?


#4

Are you accessing it thru localhost?


#5

Yes, or at least that’s what I am attempting to do. Haha

My apologies, I think I misread the question. No, my test environment has an IP that I am using through a browser to try and connect to.


#6

http:localhost/directory


#7

I went back and look at everything I had downloaded for php, and realized I didn’t download any of the extra components needed. Once I did so, I reloaded the page, and got a 500 ERROR, screenshot below.

20%20AM

Also, the file is named index.php and is located at the root directory for apache.


#8

First of all, your database design is wrong. Learn “Database Normalization” and fix your DB before you do anything.

As for your code, get rid of the duplicate connections. You only need to open it once. There is no need to manually close the connection. PHP will do it automatically.

Do not create variables for nothing.

Do not output internal system errors to the user. That info is only good to hackers.

You need to escape the DB output

In a nutshell, you have about 75% more PHP code than you need.

Since you are just starting out I would highly suggest you use PDO instead of Mysqli. Here is a tutorial to get you going. https://phpdelusions.net/pdo


#9

That is an IP address, not localhost.
@benanamen, that stuff can be dealt with AFTER the page can actually be accessed.


#10

@astonecipher My apologies, I think I misread the question. No, my test environment has an IP that I am using through a browser to try and connect to.


#11

Well the first step is just to be able to get the code working locally. So a file just like this needs to be created and, when it shows properly, we can move on to the other code issues.

index.php

<?php php_info(); ?>

#12

The fastest and easiest way to get a local dev running is to install Laragon.


#13

@astonecipher I started a new environment on AWS and have gotten that function to work here http://18.234.230.152/info.php
But still not luck for my program

@benanamen I am not very familiar with Laragon, I tried looking it up, is it an alternative to apache?


#14

I looked at the error log and this is what I saw:

[Fri Feb 01 19:21:20.163557 2019] [:error] [pid 14423] [client 73.169.82.233:63540] PHP Fatal error: Uncaught Error: Call to undefined function php_info() in /var/www/html/index.php:1\nStack trace:\n#0 {main}\n thrown in /var/www/html/index.php on line 1


#15

phpinfo();

I boo-booed


#16

I followed a different tutorial I found for setting that up anyways, and it looked correct. And even if I remove that file completely, I still get the same error, but I don’t see that function in my code at all.