php查询1个表中的2个字段

时间:2013-01-11 18:25:50

标签: php mysql joomla

我有一个包含字段A,B,C,D,E的数据库表(furcodes) 我使用此代码在Joomla网站中创建一个兑换代码系统:

    <?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;
}
?>

我使用count来检查数据库中是否存在此代码,但我还需要从“E”列获取该字段,该字段表明此代码之前是否已被使用过(值0 =未使用或1 =使用,每个代码一次使用)并创建一个如下:

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

任何帮助?


我使用的最终代码,可能对其他人有帮助。

<?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;
}
?>

2 个答案:

答案 0 :(得分:3)

根本不需要检查计数,只需定期选择E列

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

如果没有返回任何行,则表示表中不存在代码。如果返回一行,则可以检查E的返回值以查看状态。

答案 1 :(得分:0)

更改

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

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