This is not possible with the approach you are using because you are fetching data using while loop and it will only load last username
in $_SESSION
You can do it with arrays but how you will figure out which user has which session then and as you mentioned in comment What i actually want is to get td value on another page which is clicked by users
so why using $_SESSION
, you can pass what ever value to another page via link href
Change your approach to this
<?php
$sql = "SELECT * FROM users";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
$username = $row['user_name'];
$id = $row['id'];
?>
<table border='1'>
<tr>
<th>Id</th>
<th>UserName</th>
<th>Action</th>
</tr>
<tr>
<td><?php echo $id;?></td>
<td><?php echo $username;?></td>
<td><a href="pagelink.php?username=<?php echo $username;?>">Go To Page With UserName</a></td>
</tr>
<?php } ?>
So here $username
value passing to another page via link (or pass any other variable value you like to pass)
<td><a href="pagelink.php?username=<?php echo $username;?>">Go To Page With UserName</a></td>
And on next page pagelink.php
fetch username with $_GET
from link
<?php
$username = $_GET["username"];
echo $username;
//Do what ever next you wana do
?>
OR if you want to load a session, do it like this
<?php session_start();
$username = $_GET["username"];
$_SESSION['username'] = $username;
echo $_SESSION['username'];
//Do what ever next you wana do
?>
Note: mysql
library is deprecated, you should start using mysqli