I expect an another exception

Hi,

I write the wrong username:

$mysql_user = 'incorrect_username';

And I expect to see the text:

Could not connect to server.

But I see:

Could not select to database.

Why?

index.php


<?php

$mysql_host = 'localhost';
$mysql_user = 'incorrect_username';
$mysql_pass = '';
$mysql_db = 'a_database';

class ServerException extends Exception {
    
}

class DatabaseException extends Exception {
    
}

try {
    if (!@mysql_connect($mysql_host, $mysql_user, $mysql_pass)) {
        throw new ServerException('Could not connect to server.');
    } else if (!@mysql_select_db($mysql_db)) {
        throw new DatabaseException('Could not select to database.');
    } else {
        echo 'Connected';
    }
} catch (ServerException $exc) {
    echo 'Error: ' . $exc->getMessage();
} catch (DatabaseException $exc) {
    echo 'Error: ' . $exc->getMessage();
}
?>

Thank you.

P.S. This code is from the video tutorial: http://thenewboston.org/watch.php?cat=11&number=187

You’re suppressing all the errors by using the @ symbol, remove that and the exceptions will be thrown.

In the above code, on line

} else if (!@mysql_select_db($mysql_db)) {

you are using “@”.
This is giving you the error: Could not select to database.

Thank you!

Nothing has been changed.

index.php


<?php

$mysql_host = 'localhost';
$mysql_user = 'incorrect_username'; // I purposely wrote the incorrect name
$mysql_pass = '';
$mysql_db = 'a_database';

class ServerException extends Exception {
    
}

class DatabaseException extends Exception {
    
}

try {
    if (!mysql_connect($mysql_host, $mysql_user, $mysql_pass)) {
        throw new ServerException('Could not connect to server.');
    } else if (!mysql_select_db($mysql_db)) {
        throw new DatabaseException('Could not select to database.');
    } else {
        echo 'Connected';
    }
} catch (ServerException $exc) {
    echo 'Error: ' . $exc->getMessage();
} catch (DatabaseException $exc) {
    echo 'Error: ' . $exc->getMessage();
}
?>

I expect to see:

Error: Could not connect to server.

But I see:

Error: Could not select to database.