SQL Query short code assistance.

I am trying to do a check for the promotional membership. This works:

 

$promotion_membership = mysql_query("SELECT * FROM `sys_options` WHERE `Name` = 'enable_promotion_membership'");
            while($row = mysql_fetch_array($promotion_membership))
                  {   
                  $VALUE = $row['VALUE'];       
                  }
            if ($VALUE == 'on')
            echo 'promotion on';
            else
            echo 'promotion off';

 

but it is too long. I tried this:

 

$promotion_membership = mysql_query("SELECT * FROM `sys_options` WHERE `Name` =  'enable_promotion_membership' AND `VALUE` = 'on");
            if ($promotion_membership)
            echo 'promotion on';
            else
            echo 'promotion off';

 

But it always returns 'promotion off' even if the promotion is 'on'. What am I doing wrong. Any assistance would begreatly appreciated.

Thanks in advance.

caredesign.net
Quote · 13 Apr 2014

$promotion_membership = db_value("SELECT `VALUE` FROM `sys_options` WHERE `Name` = 'enable_promotion_membership'") ? 'on' : 'off';

Using that $promotion_membership will contain either 'on' or 'off'

Or you can do this.

$promotion_membership = db_value("SELECT `VALUE` FROM `sys_options` WHERE `Name` = 'enable_promotion_membership'");

$promotion_membership will be either 'on' or empty.

Or this.

$promotion_membership = db_value("SELECT `VALUE` FROM `sys_options` WHERE `Name` = 'enable_promotion_membership'") ? true : false;

In that case $promotion_membership will be either boolean true or false.


EDIT: Sorry. Typo i just fixed. I was using | instead of :   Not enough sleep.

https://www.deanbassett.com
Quote · 13 Apr 2014

Thanks again Deano. I really appreciate it. I used:

$promotion_membership = db_value("SELECT `VALUE` FROM `sys_options` WHERE `Name` = 'enable_promotion_membership'") ? true : false;
            if ($promotion_membership)
            echo 'promotion on';
            else
            echo 'promotion off';

 

as my solution.

caredesign.net
Quote · 13 Apr 2014

You could do it this was as well.

echo db_value("SELECT `VALUE` FROM `sys_options` WHERE `Name` = 'enable_promotion_membership'") ? 'promotion on' : 'promotion off';

It's a little shorter if thats all your doing with it. Unless of course you need $promotion_membership further in your code. But if all your doing is displaying a message based on it's value, then that is much easier.

https://www.deanbassett.com
Quote · 13 Apr 2014

my goal is to run a function if the promotional membership is being used. I had a brain fart because I was trying to display the results so that I could make sure that it was wroking. I will probably be using:

 

$promotion_membership = db_value("SELECT `VALUE` FROM `sys_options` WHERE `Name` = 'enable_promotion_membership'") ? $fFunction = runFunction(1) : $fFunction = runFunction(2);

caredesign.net
Quote · 13 Apr 2014

Ok.

Well i guess you learned something new today. PHP shorthand called Ternary Operators

https://www.deanbassett.com
Quote · 13 Apr 2014

the bad part is that I have been using it for a while now. I have similar codes all over the place, and even on the same page. Wanting to see the results totally through me for a loop. But thanks again, Deano.

P.S. I am still waiting for you to adopt me - lol.

caredesign.net
Quote · 13 Apr 2014
 
 
Below is the legacy version of the Boonex site, maintained for Dolphin.Pro 7.x support.
The new Dolphin solution is powered by UNA Community Management System.