Hai can any tel me , How i can get username from database where i logged in using userid . here is the code below.

if(isset($_SESSION['u_name'])){
include 'dbcon.php';
$qry = "SELECT * FROM tbl_user where user_id='".$_SESSION['u_name']."'";
$result = mysql_query($qry,$con);
if($result)
{
$row=mysql_fetch_array($result);

}
while($row=mysql_fetch_array($result))
{ 
   echo ' Welcome  ' .$row['user_name'];
} 
}

i want to display only the username of the person who logged in using his userid. The above code displays nothing.

Dani AI

Generated

The behaviour seen in this thread (no username shown or the first row missing from a result set) is usually a symptom of reading from the query result before you iterate it, and of using the old ext/mysql API. and pointed toward fixes that make the immediate problem go away. Below are practical, modern steps to fix the bug and avoid common security/compatibility problems going forward.

  • Only retrieve the column you need (for example user_name) and add LIMIT 1 when looking up the logged-in user. This is cheaper and clearer than SELECT *.
  • Do not consume a row before your loop. If you need a single username, fetch once and stop; if you need many rows, iterate directly.
  • Move away from the deprecated mysql_* functions. Use prepared statements (mysqli or PDO) to avoid SQL injection instead of manual escaping.
  • Always validate the session value (type and presence) before using it in a query, and escape output with htmlspecialchars() when echoing names into HTML to avoid XSS.

Example using PDO (prepared statement + safe output):

<?php
// assume session_start() has already run and $_SESSION['user_id'] is set
$pdo = new PDO('mysql:host=localhost;dbname=your_db;charset=utf8mb4','dbuser','dbpass',[
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
$stmt = $pdo->prepare('SELECT user_name FROM tbl_user WHERE user_id = :id LIMIT 1');
$stmt->execute([':id' => $_SESSION['user_id']]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user) {
    echo 'Welcome ' . htmlspecialchars($user['user_name'], ENT_QUOTES, 'UTF-8');
}

For reference on prepared statements and safe output: see the PHP docs for PDO prepared statements and htmlspecialchars(). Also confirm session_start() runs before checking $_SESSION so the stored id is available. Thanks to and for the troubleshooting direction in the thread.

Recommended Answers

All 7 Replies

Don't use this statement $row=mysql_fetch_array($result); twice in your code...
Just remove first one , then you will find your expected result...

Try the following:

if(isset($_SESSION['u_name'])){
include 'dbcon.php';
$qry = "SELECT * FROM `tbl_user` where `user_id`='".mysql_real_escape_string($_SESSION['u_name'])."'";
$result = mysql_query($qry,$con) or die(mysql_error());
if(mysql_num_rows($result)>0) {
    while($row=mysql_fetch_array($result))
        { 
        echo ' Welcome ' .$row['user_name'];
        }
    }
}

Note: Please use code tags.

Hi cwarn23, I am really glad bcoz ur solution to me solved the problem.
YOU r the man . Please keep doing this job for begineers like me.
Again Thanks a lot to u.

Hi any one please solve this,
I am new to php, i displayed the datas from db , via select ,
whats the problem i got is , i didnt got the first entry of the database , But it displays Rest of the elements in db.
Here is the code

<?php
/*$link = mysql_connect('localhost', 'root', ''); 
if (!$link) {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("b2bassociates");*/
include 'dbcon.php';

$qry = "select * from tbl_location order by loc_name";
$result = mysql_query($qry,$con);
if($result)
{
$row=mysql_fetch_array($result);

}
?>
<select name="sel_loc" title="Select Location">
<option value="">----Select Location----</option>
<?php 
while($row=mysql_fetch_array($result))
{ 
?>
   <option value="<?php echo $row['loc_code']; ?>" 
   >
   <?php echo $row['loc_name']; ?>
   </option>
<?php 
} 
?>

ie if i have 5 names in the above table named tbl_location , it displays only 4. Please help me.

Hi any one please solve this,
I am new to php, i displayed the datas from db , via select ,
whats the problem i got is , i didnt got the first entry of the database , But it displays Rest of the elements in db.
Here is the code

  1. <?php

  2. /*$link = mysql_connect('localhost', 'root', '');

  3. if (!$link) {

  4. die('Could not connect: ' . mysql_error());

  5. }

  6. mysql_select_db("b2bassociates");*/

  7. include 'dbcon.php';

$qry = "select * from tbl_location order by loc_name";
$result = mysql_query($qry,$con);
if($result)
{
$row=mysql_fetch_array($result);

}
?>
<select name="sel_loc" title="Select Location">
<option value="">----Select Location----</option>
<?php
while($row=mysql_fetch_array($result))
{
?>
<option value="<?php echo $row; ?>"
>
<?php echo $row; ?>
</option>
<?php
}
?>

ie if i have 5 names in the above table named tbl_location , it displays only 4. Please help me.

hi...
plz use code tags....here u have to check 2 ways....
1) u wrote order by loc_name, check location is empty or not?
2)u have to use mysql_fetch_array() only once...
try to use this below code..

<?php
/*$link = mysql_connect('localhost', 'root', ''); 
if (!$link) {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("b2bassociates");*/
include 'dbcon.php';

$qry = "select * from tbl_location order by loc_name";
$result = mysql_query($qry,$con);
?>
<select name="sel_loc" title="Select Location">
<option value="">----Select Location----</option>
<?php 
while($row=mysql_fetch_array($result))
{ 
?>
   <option value="<?php echo $row['loc_code']; ?>" 
   >
   <?php echo $row['loc_name']; ?>
   </option>
<?php 
} 
?></select>

Thank u ahmksssv,
It works good for me, I am really appreciate ur work, Thanks a lot , Keep it up.

Thank u ahmksssv,
It works good for me, I am really appreciate ur work, Thanks a lot , Keep it up.

Welcome....plz mark as solved this thread....

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.