-4

I want to fix this error. I want to get the column that name in database is role and put it into session

    $username = $_POST['username'];
    $password = $_POST['password'];

    $statment = $con->prepare("select * from users where username = ? and password = ? limit 1");
    $statment->execute(array($username,$password));
    $count = $statment->rowCount();
    $row = $statment->fetchColumn();

    if($count > 0)
    {
        $_SESSION['username'] = $username;
        $_SESSION['password'] = $password;
        $_SESSION['role'] = $row['role'];

        echo 'User Name : '.$_SESSION['username'].' , Password : '.$_SESSION['password'].' , Role : '.$_SESSION['role'];
    }
    else
    {
        echo 'Failed To Login';
    }
Qirel
  • 25,449
  • 7
  • 45
  • 62

1 Answers1

1

RTM: http://php.net/manual/en/pdostatement.fetchcolumn.php

This function returns a VALUE, not an array:

$row = $statment->fetchColumn();
  ^---your column, a string

$_SESSION['role'] = $row['role'];
                       ^^^^^^^^^^---treating your string as an array, with invalid index
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Marc B
  • 356,200
  • 43
  • 426
  • 500