Get the data of specific user in PHP

Get the data of specific user in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Retrieving Specific User Data After Authentication

When a user successfully authenticates against your application, the immediate next step is to fetch their specific profile information—name, biography, or preferences—and render it on the welcome page. The challenge lies in shifting from fetching all rows in a database table to querying for a single record based on a unique identifier. In your current setup, you are retrieving every row from the userid table using SELECT *, which returns data for every user registered in the system. This approach is fundamentally flawed for personalization because it ignores the context of the currently logged-in session.

To solve this, you must first identify a unique identifier associated with the authenticated user. Typically, this is a primary key (often named id) stored within your session after a successful password verification. Once that ID exists in memory, you can construct a targeted SQL query that filters results to include only the matching record.

Here is how you would modify your logic using the deprecated mysql_* functions found in your source material:

  1. Store the User ID in Session: During your login validation, after comparing passwords, ensure you store the specific user's primary key in the session before redirecting to the welcome page.

    if ($login_success) {
        $_SESSION['user_id'] = $info['id']; // Store the unique ID
        header("Location: welcome.php");
    }
    
  2. Query by ID: On the welcome page, retrieve only the data for that specific ID.

    $user_id = $_SESSION['user_id'];
    $data = mysql_query("SELECT Name, Email, About FROM userid WHERE id = '$user_id'");
    
    if ($info = mysql_fetch_array($data)) {
        echo "Name: " . $info['Name'];
        echo "Email: " . $info['Email'];
        echo "About: " . $info['About'];
    }
    

The critical technical decision here is the use of a WHERE clause. Without it, the database engine must iterate through the entire table index to return results, which is inefficient for large datasets. With the WHERE clause and the primary key, the database can locate the row in constant time complexity ($O(1)$), making the page load significantly faster as your user base grows.

However, relying on the mysql_* extension is a significant technical debt. This extension has been deprecated since PHP 5.5 and removed entirely in PHP 7. It lacks support for prepared statements, making it highly vulnerable to SQL injection attacks if input isn't perfectly sanitized. A safer approach involves using MySQLi or PDO with prepared statements.

Using PDO would look like this:

$pdo = new PDO("mysql:host=localhost;dbname=usersdata", "root", "");
$stmt = $pdo->prepare("SELECT Name, Email, About FROM userid WHERE id = :id");
$stmt->execute(['id' => $_SESSION['user_id']]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

This method separates the SQL logic from the data, ensuring that even if a session ID is somehow manipulated, the database treats it strictly as data rather than executable code.

If you are building a modern application and want to avoid writing these raw queries repeatedly, consider adopting a framework like Laravel. Frameworks handle session management, authentication flows, and database abstraction internally, reducing the risk of common pitfalls like forgetting to bind parameters or mishandling sessions. For more robust patterns on handling user data and authentication in PHP, resources like https://laravelcompany.com provide comprehensive guides that can help you implement these secure practices without reinventing the wheel.

The transition from fetching all users to fetching one specific user is a small change in SQL syntax but represents a massive leap in security and performance. Always ensure your session variable containing the user ID is validated before it reaches the database layer, preventing unauthorized access where an attacker might attempt to read another user's data by manipulating the session cookie.

Our network