创建cookie并设置值

时间:2017-06-22 04:58:56

标签: php cookies setcookie

我正在尝试创建cookie并根据名为'User_ID'的列设置值。然后我在浏览器中查看cookie,发现值为:

SELECT+Student_ID+FROM+%60tblaccounts%60+WHERE+Email%3D%27test%27

我希望显示User_ID,即2,而不是命令。我该怎么做呢?下面的代码是设置cookie的代码。

$sql2 = "SELECT Student_ID FROM `tblaccounts` WHERE Email='$username'"; 
$cookie_name2 = "userID";
$cookie_value2 = $sql2;
setcookie($cookie_name2, $cookie_value2);

2 个答案:

答案 0 :(得分:0)

您需要运行mysql查询并获取学生ID的值。

示例

$result =  mysqli_query($conn, "SELECT Student_ID FROM `tblaccounts` WHERE Email='$username'");
$row = mysqli_fetch_assoc($result);
$cookie_value2 = $row['Student_ID'];
$cookie_name2 = "userID";
setcookie($cookie_name2, $cookie_value2);

答案 1 :(得分:0)

$sql2 = "SELECT Student_ID FROM `tblaccounts` WHERE Email='$username'"; 
$res=$adb->pquery($sql2);
$cookie_name2 = "userID";
$cookie_value2 = $res;
setcookie($cookie_name2, $cookie_value2);

您需要执行查询。$res=$adb->pquery($sql2);