如何修复此警告"非法字符串偏移"在PHP& PDO?

时间:2016-09-16 19:42:22

标签: php pdo

我想修复此错误。我想得到数据库中的名称为role的列,并将其放入会话

    $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';
    }

1 个答案:

答案 0 :(得分:1)

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

此函数返回 VALUE ,而不是数组:

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

$_SESSION['role'] = $row['role'];
                       ^^^^^^^^^^---treating your string as an array, with invalid index