简单的MySQL查询和比较

时间:2012-04-24 09:33:34

标签: php mysql

我在堆栈溢出时看到过很多这样的问题,但我想知道为什么我的代码不起作用。它具有完美(逻辑)意义。任何人都可以向我解释一下我要编写哪些代码来实际完成我想到的if语句?

$result = mysql_query("SELECT confirmed FROM usernames WHERE username = '$username' AND password = '$password'")
if ($result==NULL)
echo 'Failure';

我在php中尝试这个。

注意:

在DB中确认设置为NULL,因此应显示最终的回显。

3 个答案:

答案 0 :(得分:1)

变量$result包含查询结果的内部表示(由mysql_query()函数返回)。要获得实际值,您需要使用其他功能,例如mysql_fetch_assoc()mysql_result()。有点像这样:

$result = mysql_query("SELECT confirmed FROM usernames WHERE username = '$username' AND password = '$password'");
$conf = mysql_result($result, 0, 0);
if ($conf == NULL){
    echo 'Failure';
}

答案 1 :(得分:0)

您需要获取一行!

$row = mysql_fetch_row($result);
if($row[0] == NULL) 
{
    echo 'failure';
}

答案 2 :(得分:0)

你可以尝试这个......

/*Add die for check if query has error */
/*Put Limit 0,1 for fetch only single row*/
$result = mysql_query("SELECT confirmed FROM usernames WHERE username = '$username' AND password = '$password' Limit 0,1") or die(mysql_error());

if($result){
  /*check if query return result or not according sql cond*/
  if(mysql_num_rows($result) == 0){
    echo "No Record Found";
    exit();
    }
  /*Assign confirmed index value to a varriable $condirmed*/  
 while($row = mysql_fetch_assoc($result)){
      $confirmed = $row['confirmed'];
   }
    if($confirmed == 'NULL'){
       echo "Failure";
    }
}