0

I have a Win 2019 (1809) server running with PHP and mySQL-DB.
IIS 10 Webserver is running, everything fine, mySQL-DB is also running.
I can connect to my mySQL-DB using phpMyAdmin.

Now my issue is that I always get "500 internal server error" ("The website cannot display the page") once I try to connect to mySQL-DB via php-file using PDO or mysqli.

Example:

<?php

define("DB_HOST", "host=xxxx:3306");
define("DB_NAME", "xxxx");
define("DB_USER", "xxxxx");
define("DB_PASS", "xxxxxxx");
define("DB_CHARSET", "utf8");


try {​​​​

$pdo = new PDO('mysql:'.DB_HOST.';
            dbname='.DB_NAME.';
            charset='.DB_CHARSET,
            DB_USER,
            DB_PASS);

    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

}​​​​
catch (PDOException $e) {​​​​

    echo "Verbindung zur Datenbank fehlgeschlagen (100).";
    die();
}​​​​

catch (Exception $e) {​​​​

    echo "Verbindung zur Datenbank fehlgeschlagen (200).";
    die();
}​​​​


?> 

When I execute the script on php.exe I get:

PHP Deprecated:  Directive 'allow_url_include' is deprecated in Unknown on line 0

Deprecated: Directive 'allow_url_include' is deprecated in Unknown on line 0
PHP Parse error:  syntax error, unexpected '$pdo' (T_VARIABLE) in C:\inetpub\wwwroot\db.php on line 12

Parse error: syntax error, unexpected '$pdo' (T_VARIABLE) in C:\inetpub\wwwroot\db.php on line 12

I don't understand, why? I'm using same structure on other projects and always works correct. Don't know what I'm doing wrong... All mysql-extensions are installed correctly. I tried with PDO and mysqli.

Thanks a lot in advance!

Sebastian
  • 3
  • 5
  • Can often mean you missed a `;` somewhere above this code – RiggsFolly Sep 06 '21 at 11:38
  • You have an invisible unicode character on the line after `try {` Remove that character and it should work – Dharman Sep 06 '21 at 11:39
  • try to re-type PDO connection string as single row: `$pdo = new PDO('mysql:'.DB_HOST.';dbname='.DB_NAME.';charset='.DB_CHARSET, DB_USER, DB_PASS);` – Slava Rozhnev Sep 06 '21 at 11:42
  • Hi, I've checked both answers but no effect, still "Parse error: syntax error, unexpected '$pdo' (T_VARIABLE) in ....". No hidden characters, I've checked with Notepad++ – Sebastian Sep 06 '21 at 11:42
  • I can see 3 zero-width-spaces there. They are definitely there. Just retype the whole thing – Dharman Sep 06 '21 at 11:43
  • Single row also had no effect. – Sebastian Sep 06 '21 at 11:43
  • Oh wow, re-typing the script was successful. But why are there hidden characters and how can I make them visible? Notepad++ didn't show up anything except LF and CR!? – Sebastian Sep 06 '21 at 12:01
  • You can use HEX editor as I did. You can also move the cursor using arrow keys and watch for seemingly stuck cursor (this is a naive approach). Generally, when you have a parse error and you can't find the mistake, just retype the whole thing to see if you had accidentally copied something invisible. – Dharman Sep 06 '21 at 12:09

0 Answers0