将SQL表select语句中的值设置为局部变量

时间:2022-09-18 20:18:42

I'm trying to take the CustomerID from the SQL select statement and then assign it to a $_SESSION variable i can use later on during the session.

我正在尝试从SQL select语句中获取CustomerID,然后将其分配给我稍后可以在会话期间使用的$ _SESSION变量。

MY CODE:

session_start();  
    $Username = $request->getParam('Username');
    $PassW = $request->getParam('PassW');


    $sql = "SELECT CustomerID FROM login WHERE Username= '$Username' AND PassW='$PassW' LIMIT 1";


    try{
        $db = new db();
        $db = $db->connect();

        $stmt = $db->query($sql);
        $stmt->execute();
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

        if(count($result) == 1){  
        $_SESSION['Username'] = $Username;
        $_SESSION['CustomerID'] = '****CUSTOMER ID FROM TABLE****';

I'm new to this so any help is appreciated :)

我是新手,所以任何帮助表示赞赏:)

1 个解决方案

#1


-1  

Sovled it.

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

needed to be

需要

$result = $stmt->fetchColumn();

#1


-1  

Sovled it.

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

needed to be

需要

$result = $stmt->fetchColumn();