Algorithm: check if a user exists in the database and return the user in JSON format

the following code takes data (email, password, user type) since $_POST() and use them to search if a user exists in the database. If yes, it sends a JSON object to the client and also stores cookies in the browser.

I have two user types and I have them stored in two tables in my database.

I need to change the database tables depending on the type of user because I have used SQL statement for both.
For that, I have implemented selection($userType) in credentials.php

SQL statement

$sql = "SELECT * FROM $credentials->userTable WHERE email='$credentials->email'";

./lib/database.php Contains the connection details of the database and connects to the database.

in Log_In.php

include "./lib/user.php";
include "./lib/credentials.php";
include "./lib/database.php";

$userDetails = json_decode($_POST("m"));
$credentials = new credentials($userDetails);
$sql = "SELECT * FROM $credentials->userTable WHERE email='$credentials->email'";
$result = $conn->query($sql);

//checking the user details
if($result->num_rows === 1) {
    $details = $result->fetch_assoc();      
    $validate = password_verify($credentials->password,$details("userPassword"));

        if($validate === TRUE){
            if($login_record === TRUE){
                $user = new user($details);
                $user->userType = $credentials->userType;
                $user->loggedInStatus = true;
                echo json_encode($user);
        else {
            echo "error";


In credentials.php

class credentials {

    public function __construct ($credentials) {

        $this->email = $credentials->email;
        $this->password = $credentials->password;
        $this->userType = $credentials->userType;
        $this->userTable = $this->selection ($credentials->userType);

    public function selection ($userType) {
        if ($userType === 'driver') {
            return "Driver_Details";

        if ($userType === 'passenger') {
            return "User_Details";


    public function setCookies ($user) {

        foreach ($user as $key => $value) {


in user.php

class user {
    public function __construct ($details) {
         $this->userName = $details("name");
         $this->userLocation = $details("country");
         $this->userId = $details("User_ID");
         $this->loggedInStatus = false;
         if (isset($details("driverVehicle"))) {
             $this->userVehicle = $details("driverVehicle");


I need to check the user type and change the tables to use more than once in different php files.

I'm looking for some ideas and tips from experts on a better way to change the database table based on user type. Mainly because I need to use that in more different php files.