1

Edit: As stated in comment section I removed try and catch statement to see the error and I was using the wrong database name. I was wondering am I suppose to remove the try and catch statement? Or just remove die() in catch and instead add some message such as echo $e->getMessage();?

In my php code whenever I write require "database.php" nothing works below that line. I am printing echo statement after require line, but it does not print. It's same if I have any code after that such as for loop or anything it won't print or go past the require line code. If I remove the require line everything works fine. What could be the reason for that?

My code:

<?php
//connecting to database
require "database.php";
echo 'after require code'; //this line won't print
// Errors on
error_reporting(E_ALL);
?>

database.php

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

try {

  $conn = new PDO("mysql:host=$servername;dbname=root", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
} catch(PDOException $e) {
  die();
}
?>
  • 2
    Most probably your code _dies_ when it reaches the `die();` after some `PDOException` occurs. Since you don't print any information about that error, you won't know what exact error happened. Remove the `try...catch` wrapper around `PDO` to allow the exception to show itself. – Smuuf Sep 26 '20 at 08:45
  • @Smuuf Thanks!! I accidently wrote `root` as database name rather actual database name. I was wondering shall I put back try and catch statement? Also, shall I write some print statement in catch block so I know if there is any error? –  Sep 26 '20 at 08:50
  • In dev or test environment you can add `var_dump($e);` before `die();` but not in production. In production exception should be writed into error log – Slava Rozhnev Sep 26 '20 at 08:55
  • @SlavaRozhnev So do you reckon to use error log as `echo $e->getMessage()`? Did you mean I need to still have `die()` after the error log? –  Sep 26 '20 at 08:57
  • Yes. If you have some logger class in your project add something like `$logger->error($e->getMessage());` and `die();` because rest of code going to be needless after DB connection error . – Slava Rozhnev Sep 26 '20 at 09:10
  • @SlavaRozhnev What do you mean by $logger? I didn't understand. Could you please explain? What class are you exactly talking about. All I have is index.php class at the moment. –  Sep 26 '20 at 09:18
  • @Sky, you can read https://www.loggly.com/ultimate-guide/php-logging-basics/ for more information – Slava Rozhnev Sep 26 '20 at 09:29
  • @SlavaRozhnev you don't really need no tutorials for this. **PHP already logs all errors**, without any effort on your part (of course unless you deliberately turned it off). – Your Common Sense Sep 26 '20 at 11:13
  • Sky, so yes - you have to remove the try and catch statement, and also remove die(), and echo $e->getMessage(); **as well**. You see, PHP is not made by fools, so just *let PHP to do the job*. All your die and echo are only interfering. – Your Common Sense Sep 26 '20 at 11:17

0 Answers0