Jump to content

Referencing Function Inside MySQL Query?


cpenk

Recommended Posts

Hi there,

 

I'm a beginner with MySQL, and I'm modifying an existing user/event sign up database. My question is, how do I refer to a function within a CASE-WHEN-THEN component of a MySQL query?

 

Specifically, I want to display our "secret"-type events only for people who are logged in. Using if-else statements with the following structure works as expected:

 

if ($session->logged_in){
}

 

I would use that same syntax within the MySQL query, but they require CASE syntax instead. So my solution thus far has been to define a one-off variable ($one_off for the sake of clarity) before the query, and make it equivalent to the result of evaluating the logged-in function. Here is how I defined the variable:

if ($session->logged_in)
{$one_off = 1;}
else {$one_off = 0;};

 

I then refer to that variable within the CASE-WHEN-THEN structure as follows, in order to restrict the events selected by the query:

SELECT * FROM `".TBL_EVENTS."` WHERE `archived` = 0 AND
CASE WHEN ('$one_off' = 1) THEN type >0
ELSE type != 11 AND type != 12
END

 

To be honest, I'm not sure what the "->" notation signifies - the session.php file included on the events page defines both a session class (within which a $logged_in variable is defined) and a $session variable. Ultimately I want to include something similar to ($session->logged_in) within the CASE structure. I'm sure there's a simple way to do this, without passing it through the extra variable. It might just be a syntax issue. Thank you in advance for your help.

Link to comment
Share on other sites

If you just want to use your $session object in the query can't you do it like this?

 

SELECT * FROM `".TBL_EVENTS."` WHERE `archived` = 0 AND
CASE WHEN ('".$session->logged_in."' = 1) THEN type >0
ELSE type != 11 AND type != 12
END

 

Do you get an error? What does it say?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.