创建 cookie 并设置值

Creating a cookie and setting the value

我正在尝试创建一个 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);

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

示例

$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);
$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);