dembilesmana's avatar

Test the account users in the database

I want to test the account users in the database (not the users in the table). so basically I can make a database with php.

how to do that in laravel?

0 likes
4 replies
tykus's avatar

You can (if the DB_USER you have connected with has appropriate privileges*) run the following:

DB::select('SELECT User FROM mysql.user')

to get a list of the database user accounts.

* I hope the DB_USER for your production web app cannot query the mysql database!

dembilesmana's avatar

thank you for the answer, but that doesn't seem to be my intention.

what I mean is to test the connection to the database, which I got like this:

if (DB::connection()->getPdo() == TRUE) {
    echo "Connection to database successful";
} else {
    echo "Connection to database failed";
}

but the code above directly retrieves data from the .env file, which causes if there is an error in the user and password cannot handle (SQLSTATE[HY000] [1045] Access denied for user 'testuser'@'localhost' (using password: YES)).

I want:

('test the connection to the database with a user and password') ? 'proceed to the next stage' : 'Enter the correct user and password';

can i test it first like the code above, before i put it in the .env file?

tykus's avatar
tykus
Best Answer
Level 104

Oh, right... just use a try...catch, catching the PDOException, and handling it directly . rather than bubbling it up to the framework:

try {
    DB::connection()->getPdo();
        echo "Connection to database successful";
} catch (PDOException $e) {
    echo "Connection to database failed";
}

... or whatever your intended flow is...

Please or to participate in this conversation.