Copy link to clipboard
Copied
Hey all,
I was just wonering if there is a way to set the WHERE clause of my query as the session variable? I've got the following so far, and have tried to go through a ton of tutorials, but it just does not seem to work. Is this even possible? I know the following is wrong, but can someone help me out with either changing the following code, or pointing me to a good tutorial? (I'm writing it with PHP)
$query = "SELECT * from members where user_id = '$_SESSION['members']";
Thanks for your help.
1 Correct answer
$member = "-1";
if (isset($_SESSION['members'])) {
$member = $_SESSION['members'];
}
$query = "SELECT * from members where user_id = $member";

Copy link to clipboard
Copied
$member = "-1";
if (isset($_SESSION['members'])) {
$member = $_SESSION['members'];
}
$query = "SELECT * from members where user_id = $member";
Copy link to clipboard
Copied
Thank you so much. That worked perfectly.
If possible, can you let me know of a tutorial that will help me understand what that code did? For example, what does the line: $member = "-1"; do?
Thanks again.

Copy link to clipboard
Copied
If possible, can you let me know of a tutorial that will help me understand what that code did? For example, what does the line: $member = "-1"; do?
Thanks again.
the line: $member = "-1"; sets the variable for $member to a value of -1 by default so that if the session variable is not set then the query will not display undesired results. It's similar to setting the value of the variable to NULL by default.

