1

When I am fetching the row it not fetch array I want to know if I want to display the user name in session what should I have to do. This is the code I am checking for user name and if user name is found returning the name of user but it could not display.

Please help me or suggest me any better way. Thanks. error code: function

public function Check_login($uname,$pass)
{
    //echo $uname;

    $check=mysql_query("select name from user where name='$uname' and   password='$pass'");
    $log=mysql_fetch_row($check);
    $abc=mysql_fetch_array($check);
    echo $abc['name'];
    if($log>0)
    {
        $_SESSION['login']=1;
        echo $_SESSION['name']=$abc['name'];
        return $_SESSION['name'];
    }
    else
    {
        return false;
    }

home page:

   <?php
   session_start();
 include('function.php');
 $user=new user();
 if(!$user->get_session())
 {
header("location:index.php");
  }

 if($_GET['q']=='logout')
 {
$user->logout();
header("location:index.php");
 }
 ?>

here is html code:

<h1>All Information</h1>
Welcome<?php echo $_SESSION['name']; ?></br>

<a href="?q=logout">Logout</a>`enter code here`
1
  • my Question is why both function is not working at the same time on same variable Commented Nov 23, 2011 at 8:42

2 Answers 2

0
$check=mysql_query("select name from user where name='$uname' and   password='$pass'");
session_start();
$abc = array();
if(mysql_num_rows($check) > 0){
    $abc=mysql_fetch_array($check); 
    if(count($abc)>0)
    {
        $_SESSION['login']=1;
        echo $_SESSION['name']=$abc['name'];
        return $_SESSION['name'];
    }
}
else
{
    return false;
}

i think this will solve your problem .. change your code with this..

1
  • It's working fine but is there any shorter way to do the same thing? Commented Nov 23, 2011 at 7:15
0

For starters, I would strongly recommend using prepared statements rather than just tossing a username / password into a query. You can bind the username and password as parameters to the query and prevent any type of sql injection. You can also bind the name that is returned to a result, which would make it easily accessible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.