Liam
Liam

Reputation: 9855

Get 1 value from MySQL table using PHP

Im trying to retrieve the correct user_id for my user who is logging in to my website...

$result = $conn->prepare("SELECT * FROM users WHERE email_address= :em AND password= :pw");
$result->bindParam(':em', $user);
$result->bindParam(':pw', $password);
$result->execute();
$rows = $result->fetch(PDO::FETCH_NUM);
if($rows > 0) {
    $_SESSION['loggedin'] = 1;
    $_SESSION['uid'] = $rows['user_id'];
}

I want to get the relevant user_id and im trying to do so using $rows['user_id']; with no luck...

Upvotes: 0

Views: 45

Answers (1)

xdazz
xdazz

Reputation: 160833

You should use PDO::FETCH_ASSOC instead of PDO::FETCH_NUM

And fetch return false when there is no result more, so you should do like:

$result->execute();
if ($row = $result->fetch(PDO::FETCH_ASSOC)) {
    $_SESSION['loggedin'] = 1;
    $_SESSION['uid'] = $rows['user_id'];
}

Upvotes: 2

Related Questions