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.

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.