Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Session Variable in WHERE clause of SQL statement

Explorer ,
Apr 16, 2010 Apr 16, 2010

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.

TOPICS
Server side applications
4.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Deleted User
Apr 16, 2010 Apr 16, 2010

$member = "-1";
if (isset($_SESSION['members'])) {
  $member = $_SESSION['members'];

}
$query = "SELECT * from members where user_id = $member";

Translate
Guest
Apr 16, 2010 Apr 16, 2010

$member = "-1";
if (isset($_SESSION['members'])) {
  $member = $_SESSION['members'];

}
$query = "SELECT * from members where user_id = $member";

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 16, 2010 Apr 16, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 16, 2010 Apr 16, 2010
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines