0

I am getting Internal Server Error when I use the following code. Do I have to change any configuration? I am using PHP version 5.2.6. I couldn't find any documentation about this issue. Please let me know. Thank you.

try {
    $dbh = new PDO($db_host1, $db_username, $db_password);
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
Nasreddine
  • 36,610
  • 17
  • 75
  • 94
nav100
  • 2,923
  • 19
  • 51
  • 89

2 Answers2

2

You have to pass DSN as first parameter of PDO constructor.

try {
    $dsn = "mysql:dbname=testdb;host={$db_host1}";
    $dbh = new PDO($dsn, $db_username, $db_password);
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
Eugene Manuilov
  • 4,271
  • 8
  • 32
  • 48
-1

I had the same 500 error after installing php-mysql.

Solution:

Restart the webserver.

I forgot to restart my webserver, which resolved the problem for me.

Apache error log said :

> PHP Fatal error:  Uncaught Error: Undefined class constant
> 'MYSQL_ATTR_INIT_COMMAND' in /var/www/html/[...].php:8 Stack trace:
> #0 /var/www/html/[...].php(27): myFunction()
> #1 {main}   thrown in /var/www/html/[...].php on line 8, referer: http://localhost/
Starsky
  • 1,829
  • 18
  • 25
Cari Bou
  • 3
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 31 '22 at 15:51
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/31425242) – Melvin Apr 02 '22 at 11:31