2015-07-06 54 views
-2
try{ 
    $query = "SELECT user_id FROM user WHERE username = '".$username."'"; 

    $result = $dbhandle->query($query,PDO::FETCH_OBJ); 
    //$stmt->bindParam(":username",$username); 

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


       while($row = $result->fetch()){ 

        $id = $row['user_id']; 


        $_SESSION['user_id'] = $id; 

        } 

       }catch(PDOException $e){ echo $e->getMessage();} 
+0

你會得到什麼錯誤? – TechWisdom

回答

0

返回USER_ID您可以使用此代碼示例代碼

$DBH = new PDO("connection string goes here"); 
    $STH - $DBH -> prepare("SELECT user_id FROM user WHERE username = '".$username."'"); 
    $STH -> execute(); 
    $result = $STH -> fetch(); 
    echo $result ["user_id"]; 
    $DBH = null; 

希望這有助於。

+0

謝謝,但我們必須使用PDO – Kd01

+0

更新爲PDO。你可以pl ch嗎? – Infocrafters