检测行中是否包含值

时间:2015-07-23 01:42:42

标签: php mysql

基本上我有一张这样的表:

|id|username|password|email|hasAlphaKey|
 1  test1    test2    -----      1
 2  test2    test3    -----      0

我想检测hasAlphaKey == 1我是如何实现这一目标的呢?

我试过这样做:

$hasAlphaKey = mysql_query("SELECT * FROM Accounts WHERE hasAlphaKey='1'");
$check_for_key = mysql_num_rows($hasAlphaKey);

if($check_for_key == 1) {
  echo 'You have an alpha key!';
}

如果使用帐户 test1 导致它在hasAlphaKey中的值为1 但是当我登录test2时,它仍会执行第echo 'You have an alpha key!';行,但该值设置为零?

我可以帮忙吗? :)

3 个答案:

答案 0 :(得分:2)

|id|username|password|email|hasAlphaKey|
 1  test1    test2    -----      1
 2  test2    test3    -----      0


$qry="SELECT * FROM Accounts WHERE hasAlphaKey=1 AND username='test2'";
$result123 = mysql_query($qry);
$check_for_key = mysql_num_rows($result123);

if($check_for_key == 1) {
  echo 'You have an alpha key!';
}

对于username ='test1',它应该回显。

对于username ='test2',它不应回显

答案 1 :(得分:1)

test1有alpha键,test2没有。所以在表中有1个带有alpha键的元素。代码按预期工作。也许你想这样做

SELECT * FROM Accounts WHERE hasAlphaKey=1 AND username='test2'

答案 2 :(得分:0)

$hasAlphaKey = mysql_query("SELECT * FROM Accounts");
$check_for_key = mysql_num_rows($hasAlphaKey);
$row = mysql_fetch_assoc($hasAlphaKey);
$hasKey = $row['hasAlphaKey'];
if($hasKey == 1) {
     echo 'You have an alpha key!';
}