Mohammed Ali
Mohammed Ali

Reputation: 1

PHP / Mysql not able to retrieve current logged in first_name from sql database

I hope you find and doing well, I would like to inform you that I am facing an issue with the created login system, as I'm not able to retrieve current logged in first_name from SQL database; as it's displaying only first record from the database if I logged in with any of the created users.

Here is the created function:

function user_data($user_id) {
    $data = array();
    $user_id = (int)$user_id;

    $func_num_args = func_num_args();
    $func_get_args = func_get_args();

    if ($func_num_args > 1) {
        unset($func_get_args[0]);
        $conn = mysqli_connect('localhost', 'root', '', 'login_system') or die($connect_error);

        $fields = '`' . implode('`, `', $func_get_args) . '`';

$data = mysqli_fetch_assoc(mysqli_query($conn, "SELECT $fields FROM `users` WHERE `user_id` = $user_id"));

    }
    return $data;
}

and here is the code to display the first name:

<div class="widget">
    <h2>Hello, <?php echo $user_data['first_name']; ?>!</h2>
</div>

For example:

1) I have created the following users: alex, billy, Carlo.

2) While login with billy or Carlo usernames; I am only getting the first record details which are related to Alex.

Could you please check and advice if there is an issue in the code and why it's displaying only first record details.

Thank you.

Upvotes: 0

Views: 58

Answers (1)

Chirag Kamat
Chirag Kamat

Reputation: 109

Possibly you are getting wrong user_id as input to query. Also there is a possibility that user_id has duplicates and not marked as unique key.

Please update the question and provide exact table structure with data.

PS: it's very important to escape your parameters before you inject your value to query. Use of pdo is advisable.

Upvotes: 1

Related Questions