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.
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.
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:
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