Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP & Databases with PDO Getting Started With PDO Managing Errors

Jeffrey Cunningham
Jeffrey Cunningham
5,592 Points

I can't break my code....

I'm following along trying to build the try catch, but for some reason when I try to make it throw an error message by adding the a's to database my code just keeps showing the object as if everything is working fine. This is my code:

<?php

// remove before flight
ini_set('disaplay_errors', 'on');

try {
    $db = new PDO('sqlite:./dataaaabase.db');
    var_dump($db);
    die();
} catch(Exception $e) {
  echo $e->getMessage();
  die();

}

?>

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">
  <title>PHP Data Objects</title>
  <link rel="stylesheet" href="style.css">

</head>

<body id="home">

  <h1>Sakila Sample Database</h1>

  <h2>Films by Title</h2>

  <ol>
      <li><i class="lens"></i>Film One</li>
      <li><i class="lens"></i>Film Two</li>
      <li><i class="lens"></i>Film Three</li>
      <li><i class="lens"></i>Film Four</li>
  </ol>

</body>

</html>

My guess is you're having the same issue as me. When it doesn't find the database, it creates a new one with that new database name. So it creates a new dataaaabase.db. and it doesn't crash because that new database exist.

13 Answers

Try removing the 'a' from Disaplay ;)

'disaplay_errors'

'display_errors'

Igor Krnjajic
Igor Krnjajic
3,820 Points

The only way I managed to break my code is when I completely changed the file path: php $db = new PDO('sqlite:./database.db'); to php $db = new PDO('sqlite:./somefolder/database.db'); and I get the following error "SQLSTATE[HY000] [14] unable to open database file" just like in the video.

Still after putting everything back the way it needs to be and just changing the name of the database, it just creates a new one without any errors...

PS Not to be rude or anything, but Chris Grazioli is right. This is not the first time I got stuck, and most of the time I was not able to find my answer here on the forum, but had to spend a lot of time searching the web. There are a lot of unanswered questions.

gianca
gianca
6,909 Points

It happens again after 1 year :)

Chris Grazioli
Chris Grazioli
31,225 Points

Shouldn't you also be able to see that there was an error in the code by the var_dump($db); not showing the object on the screen?

Jacob Tollette
Jacob Tollette
27,884 Points

I'm having the same issue... It's creating whatever terrible stuff I type in the DSN for the PDO object.

    $db = new PDO('sqlite:./sometingelse.db');

creates a file called somethingelse.db and so I never see the error. Here's the rest of the code:

<?php

// remove before flight
ini_set('display_errors', 'On');

try {
  $db = new PDO('sqlite:./sometingelse.db');
  var_dump($db);
  die();
} catch(Exception $e) {
  echo 'Sorry, the connection was not successful';
  die();
}

?>

I'm just going to move on, but I wanted to make it clear that there is some kind of error or ID10T problem for me.

Kyle Schmidt
Kyle Schmidt
9,992 Points

I have the same issue. I can't get an error message to display with or without the try/catch statement.

<?php

ini_set('display_errors', 'On');

/* try { $db = new PDO('sqlite:./dataaaaabase.db'); var_dump($db); die(); } catch(Exception $e) { echo 'Sorry the connection was not successful'; die(); } */

$db = new PDO('sqlite:./dataaaabase.db'); var_dump($db); die(); ?>

me too

+1 here. Everything displays fine if I just change the name of the file.

If I purposefully do something that makes the code fail - eg remove (both of) the single quotes from around the database filename, I get an error, but not my catch error. :(

same with me, I can't break my code

Okay, so when I went back into Workspaces a day later, it had auto-created the files for the filenames that I was entering to try and break my code. Very strange!

Aamnah Akram
Aamnah Akram
17,549 Points

Same here. Never been so disappointed at not being able to break my code :) It just creates a new database with whatever i enter.

Stephen Garner
PLUS
Stephen Garner
Courses Plus Student 11,241 Points

I'm also having the same problem. I tried deleting the workspace and launching a new one and that did not resolve the issue.

Chris Grazioli
Chris Grazioli
31,225 Points

You would think after months of people having the same issues, a Moderator or someone from treehouse would maybe find out what's going on???

David Harston
David Harston
7,261 Points

Same issue here. Igor's solution of placing the .db in a subfolder worked, but if I just change the filename there are no errors.