Search code examples
phpcookiessetcookie

Creating a cookie and setting the value


I am trying to create a cookie and set the value depending on a column called 'User_ID'. I then view the cookie in my browser and find that the value is:

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

I want the User_ID to be displayed i.e. 2 and not the command. How do I go about doing so? the code below is the code to set the cookie.

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

Solution

  • You need to run mysql query and get value of student id.

    Example

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