开发者

Cannot assign data from mysql to session array

开发者 https://www.devze.com 2023-03-08 02:50 出处:网络
I have a checklogin page and followin codes; $sql = \"select * from users where username = \'$username\' and password = \'$password\'\";

I have a checklogin page and followin codes;

$sql = "select * from users where username = '$username' and password = '$password'";
$result = mysql_query($sql) or die ( mysql_error() );
if(mysql_num_rows($result)>0) 
{
    $line = mysql_fetch_assoc($result);
    $count++;
}
if ($count == 1) 
{
    $_SESSION['login'] = "true";
    开发者_开发知识库$_SESSION['username'] = $username;
    $_SESSION['usertype'] = $line['type'];
}

And the $_SESSION['usertype'] is always 1 even though it is different in database, what am I doing wrong


I'd suggest you do a little debugging through the entire process.

  • Verify that $username and $password are what you expect them to be before you run the query:

    echo "Username: {$username} Password: {$password}<br/>";
    
  • Verify that your query is returning the right values:

    $line = mysql_fetch_assoc($result);
    echo "<pre>";
    var_dump($line);
    echo "</pre>";
    
  • If both of those tests return the expected values, then you may have to investigate your session - is it being started properly? Is it being reset somewhere?


$line is returning 1 because it is a boolean yes/no result. A result was found hence it returns 1.

You have to iterate through $line or access result set #1 directly with $line[0]['type'];

Or just fetch a single row:

Taken from the manual:

**Example #1 Fetching one row with mysql_fetch_row()**
<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($result);

echo $row[0]; // 42
echo $row[1]; // the email value
?>

In you case, if you changed your select to "select type from users .... " instead of "select *"

$line[0]
0

精彩评论

暂无评论...
验证码 换一张
取 消