PHP PDO connection help

Discussion in 'PHP' started by Gurbrinder, May 13, 2013.

  1. #1
    I am new to PHP and PDO and trying to make a connection using PDO.

    <?php 
    try
    {
    $conn = new PDO('mysql:host=localhost;dbname=test','root','');
    }
    catch(PDOException $e)
    {
    echo $e->getMessage();
    }
    ?>
    PHP:
    The above code makes the connection even with non-existing database username like rot or abc instead of displaying something like "rot or abc database username don't have permission to access test database".

    I want to display error in this case.
     
    Gurbrinder, May 13, 2013 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    HuggyStudios, May 13, 2013 IP
  3. Gurbrinder

    Gurbrinder Member

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    I have checked and read that document. In case I put the wrong password , i got the access denied message but what if database username doesn't exist and someone tries to connect with that non-existing database username? He might have misspelled the word and wrote rot or oot instead of root? I want to display error in this case.
     
    Gurbrinder, May 14, 2013 IP
  4. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #4
    Try this:

    $db_myHost = "localhost";
    $db_myUser= "";
    $db_myPassword = "root";
    $db_myDatabase = "test";
     
     
    $dbconn = new PDO('mssql:host='.$db_myHost.';dbname='.$db_myDatabase, $db_myUser, $db_myPassword);
     
    try
      {
      $dbPDO = new PDO('mssql:host='.$db_myHost.';dbname='.$db_myDatabase, $db_myUser, $db_myPassword);
      $dbPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      }
    catch  (PDOException $e)
      {
        echo "Error!: " . $e->getMessage() . "
    ";
        die();
      }
     
    
    PHP:
     
    scottlpool2003, May 14, 2013 IP
  5. Gurbrinder

    Gurbrinder Member

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #5
    I found that something is wrong with database 'test'.
    When I perform the same task on other database say 'abc' then everything works well but when i perform the same task on database 'test' it doesn't show error "access denied".

    Sorted out the problem by deleting/editing privileges and users.

    Thanks
     
    Last edited: May 14, 2013
    Gurbrinder, May 14, 2013 IP