-1

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);
Sudheesh R
  • 1,767
  • 3
  • 23
  • 43
hello
  • 29
  • 1
  • 8
  • You need to execute the SQL query? You're literally creating a string with the SQL query and store that string. Not sure what you expect to happen here? – M. Eriksson Jun 22 '17 at 05:03
  • I have recently come back to using sql and can not remember how to execute a query, could you help me with that? – hello Jun 22 '17 at 05:13
  • @MagnusEriksson I changed it so the line says $result2 = mysqli_query($connection, "SELECT Student_ID FROM `tblaccounts` WHERE Email='$username'"); – hello Jun 22 '17 at 05:14
  • _"can not remember how to execute a query"_ - Then simply do a google search and try one of the hundreds of thousand examples you'll find. No reason to write _yet another_ example. – M. Eriksson Jun 22 '17 at 05:18
  • Ok, so you've changed it. What happens? Does it work? If you don't know how to go on from there, please check the manual. – M. Eriksson Jun 22 '17 at 05:22

2 Answers2

0

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);
shubham715
  • 3,324
  • 1
  • 17
  • 27
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);

You need execute the query.$res=$adb->pquery($sql2);

mayank
  • 442
  • 4
  • 12
  • Where did `$adb` come from? – M. Eriksson Jun 22 '17 at 05:09
  • I am using $adb object, It is only for reference, If you are using mysqli than write `$con=mysqli_connect("host","username","password","database name"); mysqli_query($con,$sql2);` Please refer this [link](https://www.w3schools.com/php/func_mysqli_query.asp) – mayank Jun 22 '17 at 05:35
  • Add that info in your answer. Just adding a magic variable and tell to OP to use it isn't particular helpful, since the OP might not have a clue what it is. Answers needs to be explicit. – M. Eriksson Jun 22 '17 at 05:40