Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Database always connects despite introducing an exception

Featured Replies

Hello, it;s been a while. :)

 

I wonder if someone can help.

I have some code. I'm attempting to connect to a database via SQLite. Actually the database always connects. I'm trying to trigger an exception in a try catch blog but no matter what I do (deliberately mispelling the database name) to trigger the exception, the connection message will always show up.

Here's my code.

<?php
 
try {
    $db = new PDO("sqlite:" . __DIR__ . "/database.db");
    var_dump($db);
} catch (Exception $e) {
    echo "Unable to connect to the database";
    exit;
 
}
 
echo "Connected to the database successfully";
?>

So what happens is instead of blocking the connection if I use a different name it will create a new empty file of that name.

http://www.jonniegrieve.co.uk/jg-lab.co.uk/medlib-sql/inc/connection.php

The code is sound. Is there some reason the code is doing this that I should know of? Thanks

Edited by webDesignerJon

  • Author

Thanks for your reply :)

 

But why wouldn't it output the copy of the database then? Why an empty file of the incorrect filename? I should clarify the filename in the code is actually the correct one. I should have posted an incorrect one. In which case surely an exception should then be triggered?

 

I have seen one instance in another server location where an incorrect file name triggers an exception, no connection to the database. However this is not the case on localhost or my own web server.

 

Thanks. :-)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.