i am actually working for showing online for loged in user. This is my code:

    $qt="select userid from plus_login where tm > '$tm' and status='ON'";
    $result = mysql_query($qt);
    while($row=mysql_fetch_array($result))
    {
        $uid = $row['userid'];
        $qt1="select first_name, last_name from signup where user_id='$uid'";
    $result1 = mysql_query($qt1);
    while($rows=mysql_fetch_array($result1))
    {
            $_SESSION['name']=$rows[0]." ".$rows[1];
            echo $_SESSION['name']."<br>";
    }

    }

This above code is working but i want all $_SESSION['name'] which is in loop and displaying different values fetched from database to be shown in another pages. When i echo $_SESSION['name'] in another, it shows only one value. I think i need to store all value that is fetched from database in array form. How can i do it. Please help me. Thanks in advance.

Dani AI

Generated

The original code overwrites a single session key on each loop iteration, so only the last name survives. was right to suggest storing multiple values under one session key; the safest, more maintainable pattern is to collect the rows into an array (or store just the user IDs) and then write that array to the session once. Also, session handling and queries can be simplified and made safer by using a single JOIN and prepared statements instead of nested mysql_* calls.

Example (PDO, collect IDs to keep session small):

<?php
session_start();

$pdo = new PDO('mysql:host=localhost;dbname=app;charset=utf8mb4', 'dbuser', 'dbpass', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$sql = "
  SELECT p.userid
  FROM plus_login p
  JOIN signup s ON p.userid = s.user_id
  WHERE p.tm > :tm AND p.status = 'ON'
";
$stmt = $pdo->prepare($sql);
$stmt->execute([':tm' => $tm]);

$_SESSION['online_ids'] = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    array_push($_SESSION['online_ids'], (int)$row['userid']);
}

Retrieve on another page (must call session_start() there too):

<?php
session_start();
if (!empty($_SESSION['online_ids'])) {
    // either fetch names with a single SELECT ... WHERE user_id IN (...)
    // or loop and fetch each name, always escaping output:
    echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . "<br>";
}

Additional notes and best practices:

  • session_start() must run before any output on every page that reads/writes the session.
  • Prefer storing IDs (minimal, stable) and resolving names when rendering to avoid large session payloads.
  • Use one JOIN query to avoid N*N queries for performance.
  • Switch from deprecated mysql_* to PDO or mysqli and use prepared statements to prevent SQL injection.
  • Sanitize output with htmlspecialchars to avoid XSS.
  • For real-time "online user" lists at scale, consider a cache (Redis/memcached) or a dedicated presence table instead of storing large lists in PHP sessions.

Recommended Answers

All 4 Replies

why no any reply.

commented: because question is not clear.. +0
Member Avatar for Member #120589

try

$_SESSION['name'][] =$rows[0]." ".$rows[1];

However, it's difficult to see why there would be more than one user with a specific id.

no actually $rows[0] and $rows[1] are first name and last respectively.

Member Avatar for Member #120589

No you don't understand me:

You use 'while' loops in both cases, I fail to see why each resultset should contain more than one record.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.