# [SOLVED] how to display user name on page after login using user no id?



## rsage

hello, sorry if this is duplicate question, i did try to find the answer in internet and use it.. but i still can make it done... here my problem... i want to know how to display name when login using no id? it like welcome screen for the user after they login using their no id. here my code and sorry if it messed up...
my login.php



PHP:


   <?php


    $host="localhost"; // Host name
    $username="root"; // Mysql username
    $password="1234"; // Mysql password
    $db_name="vronline"; // Database name
    $tbl_name="user_information"; // Table name

    // Connect to server and select databse.
    $con = mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];

    // To protect MySQL injection (more detail about MySQL injection)
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);

    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);


    $sql="SELECT * FROM $tbl_name WHERE user_id='$myusername' and user_password='$mypassword'" ;
    $result=mysql_query($sql);

    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);

    // If result matched $myusername and $mypassword, table row must be 1 row

    if($count==1){

    // Register $myusername, $mypassword and redirect to file "login_success.php"
    session_register("myusername");
    session_register("mypassword");

     $row=mysql_fetch_array($result);
                            if ($row['group_id']==0){
                            header("location:../user.php");
                            }
                            elseif ($row['group_id']==1) {
                            header("location:../admin.php");
                            }
    //header("location:../menu.php");
    }


    else {
    echo "Wrong Username or Password";
    }
    ?>

this is my user.php



PHP:


<?php
    session_start();
    if(!session_is_registered(myusername)){
    header("location:../login_main.php");
    }

    ?>

  <?php
    include ('php/dbconnect.php');

    $matric = $_session['myusername'];

    mysql_query("SELECT name FROM user_information WHERE user_id=".$_SESSION['myusername']);


    ?>
    <html>
    <head>
    <title>user</title>
    <link href="source/loginstyle.css" rel="stylesheet" type="text/css" media="all" />
    <link href="source/kepala.css" rel="stylesheet" type="text/css" media="all" />
    <link href="source/content.css" rel="stylesheet" type="text/css" media="all" />
    <link href="source/menuBox.css" rel="stylesheet" type="text/css" media="all" />
    <link href="source/bottomfoot.css" rel="stylesheet" type="text/css" media="all" />
    <link href="source/accessibility_foundicons.css" rel="stylesheet" type="text/css" media="all"/>
    <link href="source/general_foundicons.css" rel="stylesheet" type="text/css" media="all" />

    <script src="source/Chart.js"></script>
    </head>
    <body>

    <div>
    <!--head-->
    <div class= "kepala">


    <h1>
    <a href="#"><span class="foundicon-smiley"></span></a> <?php $row['name'];  ?>
    </h1>


    </div>


    <div class= "content">


    <h4>content</h4>

    <div class="menu">
    <div class="menu_box_list">
                    <ul>
                        <li><a href="php/displayuseronly.php"><span class="foundicon1-person"></span> manage user<i class="manageuser"> </i><div class="clear"></div> </a></li>
                        <li><a href="userAddvehicle.php"><span> </span>vehicle manager <i class="vehicle manager"> </i><div class="clear"></div> </a></li>
                          <li><a href="#"><span class= "foundicon-page"></span> report <i class="Report"> </i><div class="clear"></div> </a></li>
                        <li><a href="#"><span class= "foundicon-settings"></span> log out<i class="settings"> </i><div class="clear"></div> </a></li>
                    </ul>
                    <div class="clear"></div>
                </div>


    </div>
    <div class = "2nd_menu">

    </div>


    </div>

    <div class= "Bfooter">
    <h4></h4>



    </div>

    </div>

    </body>
    </head>
    </html>

i cant solve around this code

> mysql_query("SELECT name FROM user_information WHERE user_id=".$_SESSION['myusername']);

how i can make it appear name instead of user_id?
sorry i am not really good about this


----------



## DDAoN

*Re: how to display user name on page after login using user no id?*

Doing a quick glance at your code, I do see a...somewhat...problem with it.


PHP:


    session_register("myusername");
    session_register("mypassword");

The use of session_register() isn't wrong, but it is depreciated. You really should us $_SESSTION[] instead. It's definitely recommended that you update the code to use the newer version:


PHP:


    $_SESSION["myusername"] = $myusername;
    $_SESSION["mypassword"] = $mypassword;

Furthermore, in order to set a session variable, you have to first start the session. So, add "session_start();" before the session_register() but still in the IF statement. With my recommendations, your IF statement for a valid login should look like:


PHP:


    if($count==1) {
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    session_start();
    $_SESSION["myusername"] = $myusername;
    $_SESSION["mypassword"] = $mypassword;

    $row = mysql_fetch_array($result);
    if($row['group_id']==0) {
     header("location:../user.php");
    } else if($row['group_id']==1) {
     header("location:../admin.php");
    }
    //header("location:../menu.php");
   } else {
    echo "Wrong Username or Password";
   }

That's just a quick glance (I hope session_start() is all that's missing), but I'll take a closer look after my lunch. ^^'


----------



## rsage

*Re: how to display user name on page after login using user no id?*

thanks for the advice... i follow your instruction... i manage to login ... how to display the name like display name on welcome screen? 

i dont know how to fix this in my user.php file


> <?php
> include ('php/dbconnect.php');
> 
> $matric = $_session['myusername'];
> 
> mysql_query("SELECT name FROM user_information WHERE user_id=".$_SESSION['myusername']);
> 
> 
> ?>


so i can display name instead of ID here, i am not sure if this is the right way to call back the name variable


> <a href="#"><span class="foundicon-smiley"></span></a> welcome <?php $row['name']; ?>


i log in using ID not the name. but i want to display the name on the page after login is success.. right now i cant display the user name on the page... 

sorry to disturb your lunch time haha... right now is 2.28 am at my place... haha.. cant sleep yet even tomorrow i have class.. -_-'.. hehe


----------



## DDAoN

*Re: how to display user name on page after login using user no id?*

My apologies, I hadn't had a chance to see if you had already called it in the user.php file or not.

Another recommendation, update from mysql to mysqli (mysql is depreciated and due to be removed from PHP).

As for calling it, you have the query set-up, you just need to output it. Give this a try:


PHP:


$query = mysql_query('SELECT name FROM user_information WHERE user_id = ' . $_SESSION['myusername'] . ';');
$name = mysql_fetch_assoc($query);
echo '<a href="#"><span class="foundicon-smiley"></span></a> welcome ' . $row['name'] . '!';

I think that's the correct structure for mysql, it's been a while. ^^'

The idea is that the mysql_query() gets raw results that are not understandable by a browser. So, you use mysql_fetch_assoc() to format the query results into a multi-dimensional array, which can then be called like any other array. It's similar to mysqli, if I recall correctly (I've been using PDO lately). Major difference between mysql and mysqli is that with mysql, when you open a connection, it stays open until timeout or your code closes it. In mysqli, you create a variable with the connection information, and there is a "micro-connection" that automatically closes once the query is done.


----------



## rsage

*Re: how to display user name on page after login using user no id?*

*ok.. i just try that.. it show this "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\web2\user.php on line 15"
why?
i just make small change



<?php
include ('php/dbconnect.php');

$matric = $_SESSION['myusername'];

$query = 'SELECT name FROM user_information WHERE user_id = ' . $_SESSION['myusername'] . '';
$result=mysql_query($query);
$row=mysql_fetch_array($result);

?>

Click to expand...

it show warning at 
$row=mysql_fetch_array($result);
*


----------



## rsage

*Re: how to display user name on page after login using user no id?*

at last now its working... i manage to make it display the user name... the new problem now that it display wrong user sesion name.. why?
this is the only change that i make in user.php


PHP:


$sql = "SELECT * FROM user_information WHERE user_id = '$myusername'";
$row = mysql_fetch_assoc(mysql_query($sql));
foreach($row as $a=>$b) $$a = $b;




PHP:


<?php echo "$name";  ?>

the problem now is that it display wrong user name.. not the user that log in.. what i should look?


----------



## DDAoN

*Re: how to display user name on page after login using user no id?*

The code segment you have there doesn't display the results of the query.

After looking at it a bit more (and looking at some of my old projects), I think I found the solution:


PHP:


 $sql = "SELECT * FROM user_information WHERE user_id = '" . $myusername . "';";
 $query = mysql_query($sql);
 while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
  echo '<a href="#"><span class="foundicon-smiley"></span></a> welcome ' . $row['name'] . '!';
 }

I used your updated query and separated the query from the execution (as you did), though this separation isn't needed.

I've built a dummy database and verified that is $myusername is properly defined, it pulls the correct results. I still recommend you update all your SQL to mysqli, but that's probably a project for after it's working.


----------

