Hello guys i am trying to get user id by username using PDO and mySQL... My codes are:
<?php
if(isset($_POST['log']))
{
$logname = @$_POST["logname"];
$logpass = @$_POST["logpass"];
$sql = "SELECT username, password FROM users WHERE username=? AND
password=? ";
$query = $conn->prepare($sql);
$query->execute(array($logname,$logpass));
if($query->rowCount() >= 1) {
$_SESSION["activeuser"] = true;
$_SESSION ["username"] = $logname;
//here is starting my problem
$findid = $conn->prepare("SELECT id FROM users WHERE username= :username");
$findid->bindParam(':username', $logname);
$findid->execute();
$_SESSION ["userid"] = $findid;
header("Location:index.php");
}
else {echo "incorrect username or password";}
}?>
This is my login.php, evrything is working good until $findid... I am trying to bind $_SESSION["userid"] = //The id of user selected from database..
if you have any other ideas you can share thank you for reading :)
Edit: The problem is solved thanks for " Eric Lam " .. Just after $findid->execute();
I edited $_SESSION ["userid"] = $findid;
to->
$result = $findid->fetch();
$_SESSION ["userid"] = $result[0]; :) :)
答案 0 :(得分:-1)
PDO::Prepare
returns a PDOStatement
. you cannot assign a PDOStatement
to the Session.
You need to bind the results to a variable.
Try
$findid->execute();
$result = $findid->fetch();
$_SESSION ["userid"] = $result[0];