简体   繁体   English

简单的MySQL查询和比较

[英]Simple MySQL query and comparison

I've seen many questions like this on stack overflow, but I want to know why my code won't work. 我在堆栈溢出时已经看到很多类似的问题,但是我想知道为什么我的代码无法正常工作。 It makes perfect (logical) sense. 这是完美的(逻辑)意义。 Can anyone please explain to me what code I would have to write to actually complete the if statement I have in mind? 谁能给我解释一下我要真正完成if语句必须编写什么代码?

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

I'm attempting this in php. 我正在php中尝试。

NOTE: 注意:

confirmed in the DB is set to NULL, so the final echo should display. 在数据库中确认后将其设置为NULL,因此应显示最终回显。

The variable $result contains an internal representation of the result of the query (as returned by the mysql_query() function). 变量$result包含查询$result的内部表示形式(由mysql_query()函数返回)。 To get the actual value you need to use another function, like mysql_fetch_assoc() or mysql_result() . 为了获得实际值,您需要使用另一个函数,例如mysql_fetch_assoc()mysql_result() Kind of like this: 有点像这样:

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

You need to fetch a row! 您需要获取一行!

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

You can try this.... 你可以试试这个。

/*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";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM