SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot 8Observer8's Avatar
    Join Date
    Jul 2013
    Location
    Russia
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I expect an another exception.

    Hi,

    I write the wrong username:
    Code:
    $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 Code:
    <?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

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You're suppressing all the errors by using the @ symbol, remove that and the exceptions will be thrown.

  3. #3
    Non-Member
    Join Date
    Sep 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Zealot 8Observer8's Avatar
    Join Date
    Jul 2013
    Location
    Russia
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you!

    Nothing has been changed.

    index.php
    PHP Code:
    <?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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •