简体   繁体   中英

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?

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

I'm attempting this in php.

NOTE:

confirmed in the DB is set to NULL, so the final echo should display.

The variable $result contains an internal representation of the result of the query (as returned by the mysql_query() function). To get the actual value you need to use another function, like mysql_fetch_assoc() or 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";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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