0

I just went to upload the 1st version of a new project I built on the host. To be clear, everything were ready to start working but finally all I got was :

HTTP ERROR 500

I know what does this mean.

My project is 100% handmade with PHP, no WP or other CMS/platform, and there is no Apache file (.htaccess will be added later).
But there is a error_log file, which always have the same line :

[19-Jan-2017 12:59:06 America/Chicago]
PHP Parse error:  syntax error, unexpected '$sgbd' (T_VARIABLE)
in /path/to/the/file.php on line 1

It refers to my database connexion file :

<?php
$sgbd = "mysql";
$host = "localhost";
$database = "dont_worry";
$charset = "utf8";
$user = "this_is_not";
$password = "my_infos";
$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
];
try {
    $bdd = new PDO('' . $sgbd . ':host=' . $host . ';dbname=' . $database . ';',
'' . $user . '', '' . $password . '', $options);
} catch (Exception $e) {
    die('Erreur : ' . $e->getMessage());
}
?>

This is the 1st file called by my pages, right after session_start();. I really don't get what can be wrong.
I was not prepared to this, thanks for helping !

EDIT : I "solved" the problem, but don't know how it happened. The file was automatically modified on upload, deleting the space between the begining of the PHP code and my first variable :

1 | <?php$sgbd = "mysql";

I verified this file and modified it many times to try to find a solution. The only way was to delete and re-upload the complete project.

AymDev
  • 6,626
  • 4
  • 29
  • 52
  • You may have an older version of PHP. If you have command line access to your server run `php --version` otherwise, add a `phpinfo();exit;` in the first line of your code to see the version number and send it to us. – Ibu Jan 19 '17 at 20:02
  • @Ibu I don't have command line access, but there is a cPanel which allows me to choose the PHP version. After verifying I am on PHP 7.1 – AymDev Jan 19 '17 at 20:06
  • When you don't know where to start tracking down an error, start troubleshooting from scratch. Remove all files from your WWW root and see if you still get the error. Create a blank `index.php` file and see if you still get the error. Put `` within your `index.php` file and see if you still get the error.... etc., keep building it back up until you see the error.... you should get the idea... some basic troubleshooting. Alternatively, you can do the same process in reverse, methodically stripping out code until the 500 error clears. – Sparky Jan 19 '17 at 20:09
  • thanks @Sparky , I'll try that if I have no other solution. I checked the post you linked and don't find anything at the moment – AymDev Jan 19 '17 at 20:15
  • Basic troubleshooting... it should be your first resort, not your last. I mean, we have absolutely nothing here... any answers would be blind guesses, so the responsibility falls to you to get the basic troubleshooting done before we can even begin to help. – Sparky Jan 19 '17 at 20:21
  • @Sparky I supposed there was something simple I was missing (as the server part is mostly unknown to me). Anyway I hope to find the reason soon and will edit at this moment. – AymDev Jan 19 '17 at 20:24

1 Answers1

3

There's no syntax error in the quoted source with the exception that if you are running this code on PHP 5.3 or earlier, then this:

$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
];

will cause syntax error as this short array syntax is available since 5.4.

Aside from the above, the following line:

$bdd = new PDO('' . $sgbd . ':host=' . $host . ';dbname=' . $database . ';',
               '' . $user . '', '' . $password . '', $options);

is over-engeneered. There's absolutely no point of doing '' you typed a lot as this simply equals to empty string. Second row also shows that you got problem understanding the difference between variable and variable's value. This should be easily written as:

$bdd = new PDO("{$sgbd}:host={$host};dbname={$database};", $user, $password, $options);

Aside from the above it should not be the cause of 500 you are facing.

EDIT

If you can invoke PHP from command line (on most linuxes it comes as separate package (i.e. php-cli so you may need to install it first)), then you can check the syntax of your file using -l switch:

$ php -l file.php

if there's any issue, then you will see the error message. If you see just Errors parsing file.php but no details, add -d error_reporting=E_ALL to the argument list:

$ php -d error_reporting=E_ALL -l file.php

Alternatively, you can simply try to access your file directly in browser (if it is not in document root, just copy it to make it available - it does not matter calling it alone makes no sense - you just need to ensure you are looking at right file - if you do, then syntax error will be logged. If this is wrong file, then nothing should happen really.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141