Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PDO新手正在努力将我的结果id放入变量_Php_Mysql_Pdo - Fatal编程技术网

Php PDO新手正在努力将我的结果id放入变量

Php PDO新手正在努力将我的结果id放入变量,php,mysql,pdo,Php,Mysql,Pdo,我的问题是,我正在查询如何从登录请求的行中获取id,并将$\u SESSION name设置为该行的id,但我不确定如何正确获取该id,因为我通常的方法似乎不起作用 所以我在努力 if (isset($_POST['username']) && ($_POST['password'])) { $username = trim($_POST['username']); $username = strtolower($username); $password = trim($_POS

我的问题是,我正在查询如何从登录请求的行中获取id,并将$\u SESSION name设置为该行的id,但我不确定如何正确获取该id,因为我通常的方法似乎不起作用

所以我在努力

if (isset($_POST['username']) && ($_POST['password'])) {
$username = trim($_POST['username']);
$username = strtolower($username);
$password = trim($_POST['password']);
$password= hash('sha256', $password);
$stmt = $dbh->prepare("SELECT `id` FROM `1_users` WHERE username=? AND password=? LIMIT 1");
$stmt->bindValue(1, $username, PDO::PARAM_STR);
$stmt->bindValue(2, $password, PDO::PARAM_STR);
$stmt->execute();
$row = $stmt->rowCount();
if ($row) {
// Match
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$_SESSION['user'] = $result['id'];
$_SESSION['logged_in'] = TRUE;
$_SESSION['ip'] = hash('sha1', "{$_SERVER['REMOTE_ADDR']}");
echo $result['id'];
//header ("location: staff.php");
}
不幸的是,
$result['id']
作为未索引索引出错。。 和
echo$result['id']未输出任何内容。但是我知道找到了该行,那么在尝试将该行的id放入变量时,我做错了什么?

因此
fetchAll()
返回一个行数组(即使只有一个行),这些行本身就是一个列数组,因此您需要:

$_SESSION['user'] = $result[0]['id'];
或者如注释中所述,使用
fetch()

它将一行作为列数组返回,那么这将起作用:

$_SESSION['user'] = $result['id'];
if(设置($\u POST['username'])和&$\u POST['password'])){
$username=trim($_POST['username']);
$password=trim($_POST['password']);
$password=hash('sha256',$password);
$stmt=$dbh->prepare(“从用户名=?和密码=?”的1_用户中选择id”);
$stmt->execute([$username,$password]);

$id=$stmt->fetchColumn();//将此
$stmt->fetchAll(PDO::FETCH_ASSOC);
更改为此
$stmt->FETCH(PDO::FETCH_ASSOC);
。这很有效!我知道这一定是件小事,谢谢!执行“$stmt->有什么好处吗([$username,$password]”);`并且不单独绑定值?这些值不绑定为字符串吗?
$_SESSION['user'] = $result['id'];
if (isset($_POST['username']) && $_POST['password']) {
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);
    $password = hash('sha256', $password);
    $stmt = $dbh->prepare("SELECT id FROM 1_users WHERE username=? AND password=?");
    $stmt->execute([$username,$password]);
    $id = $stmt->fetchColumn(); // <-- here you go
    if ($id) {
        $_SESSION['user'] = $id;
        $_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
        header ("location: staff.php");
        exit;
    }
}