John
John

Reputation: 227

php query for 2 fields from 1 table

I have a database table (furcodes) with fields A, B, C, D, E i use this code to create a redeem code system in a Joomla site:

    <?php
$redeem_code = JRequest::getString('redeem_code', '', 'post');
$db =& JFactory::getDBO();
$query = "
    SELECT COUNT(`B`)
        FROM `furcodes`
        WHERE `B` = '$redeem_code' ;
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count ) {

  $mainframe =& JFactory::getApplication();
  $mainframe->enqueuemessage('You have succesfully redeemed this fur code');
  return false;
} else {
    $mainframe =& JFactory::getApplication();
  $mainframe->enqueueMessage(JText::_('Invalid code. Please check and try again.'), 'error');
  return false;
}
?>

I use the count to check if this code exists in the database but i also need to get the field from column "E" that states if this code has been used before or not (values 0=not used or 1=used , one use for every code) and create an if like this:

    if $used == 1
 echo "This code has been used"

Any help?


Final code i used, it might help someone else.

<?php
$your_fur_code = JRequest::getString('fur_code', '', 'post');
$db =& JFactory::getDBO();
$query = "SELECT `E` FROM `furcodes` WHERE `B` = '$your_fur_code'";
$db->setQuery($query);
$code_used = $db->loadResult();
if ( $code_used == "1" ) {
  $mainframe =& JFactory::getApplication();
  $mainframe->enqueueMessage(JText::_('The code has already been used.'), 'error');
  return false;
} if ( $code_used == "0" ) {
  $mainframe =& JFactory::getApplication();
  $mainframe->enqueuemessage('You have succesfully redeemed this code');
  return false;
} else {
    $mainframe =& JFactory::getApplication();
  $mainframe->enqueueMessage(JText::_('Invalid code. Please check and try again.'), 'error');
  return false;
}
?>

Upvotes: 0

Views: 141

Answers (2)

Mike Brant
Mike Brant

Reputation: 71384

There is no need to check for the count at all, just do a regular select for column E

SELECT `E` FROM `furcodes` WHERE `B` = '$redeem_code'

If no rows are returned, you know the code didn't exist in table. If a row is returned, you can then check the returned value of E to see the status.

Upvotes: 3

swapnesh
swapnesh

Reputation: 26732

Change

$query = "SELECT COUNT(`B`) FROM `furcodes` WHERE `B` = '$redeem_code' ;";

TO

$query = "SELECT COUNT(`B`) FROM `furcodes` WHERE `B` = '$redeem_code'";

Upvotes: 0

Related Questions