Php PDO登录不';不行?
所以几天前,我在创建登录时遇到了问题,该线程上的一个人建议我使用预先准备好的语句 所以我开始使用PDO查找准备好的语句,并用它创建了register脚本。但是,每次登录时,我都会重定向回index.php file1.php:Php PDO登录不';不行?,php,mysql,pdo,Php,Mysql,Pdo,所以几天前,我在创建登录时遇到了问题,该线程上的一个人建议我使用预先准备好的语句 所以我开始使用PDO查找准备好的语句,并用它创建了register脚本。但是,每次登录时,我都会重定向回index.php file1.php: <?php $username=$_POST['username']; $password=$_POST['password']; $db = new PDO('mysql:host=localhost;dbname=ismsite', 'db_username',
<?php
$username=$_POST['username'];
$password=$_POST['password'];
$db = new PDO('mysql:host=localhost;dbname=ismsite', 'db_username', 'db_password'); //this works
$result = $db->prepare("SELECT * FROM users WHERE username=:username AND password=:password");
$result->bindParam(':username', $username);
$result->bindParam(':password', $password);
$result->execute();
$row = $result->fetch(PDO::FETCH_NUM);
if($row > 0) {
session_start();
$_SESSION['userid'] = $row['user_id']; // Initializing Session
$_SESSION['voornaam'] = $row['Voornaam']; // Initializing Session
$_SESSION['achternaam'] = $row['Achternaam'];
$_SESSION['adres'] = $row['adres'];
$_SESSION['email'] = $row['email'];
$_SESSION['username'] = $row['username'];
$_SESSION['password'] = $row['password'];
}
header("location: profile.php");
exit();
?>
在profile.php上,我有一小段代码检查是否存在具有user_id的会话。否则,您将返回index.php。这表明我的userid会话没有被设置,而我正在设置它。要么选择与插入的工作方式不同,要么我犯了一个愚蠢的错误
有人能帮忙吗?看起来我犯了一个错误,在以下情况下使用了FETCH\u NUM而不是FETCH\u ASSOC:
$row = $result->fetch(PDO::FETCH_ASSOC);
过去是
$row = $result->fetch(PDO::FETCH_NUM);
自从您在该条件块中启动会话以来,它就失败了。好的,我已经自己修复了它。我必须将FETCH_NUM更改为FETCH_ASSOCStart,方法是删除重定向,var_转储
$row
中返回的内容,然后从那里开始工作。我建议也做if($row)
,因为如果成功,它将是一个数组(和FETCH_NUM
返回一个索引数组-从你以后如何使用$row
我想你想要FETCH_ASSOC
作为你的获取模式。或者发布一个答案并获取它,或者删除这个问题。MatsLindh,是的,我检查了我的旧代码,找到了FETCH_ASSOC并将其更改为FETCH_NUM。无论如何,谢谢