php - How do I properly determine if an array that is returned is false? -


this question has answer here:

i attempting build password-protected area on site. however, in if statement, i'm having trouble determining if array empty or not. when code run, regardless of put user/pass fields, it's testing true. reference, database being accessed has 1 row, containing 1 user/pass combo.

function verify(){    $dbhost = "host";   $dbname = "db";   $dbuser = "user";   $dbpass = "password";     if (isset($_session['valid_user']))return true;    $user_name = $_post["user_name"];    $password = $_post["password"];    if ($user_name && $password){       try{        $conn = new pdo("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);     }     catch(pdoexception $pe){       die('connection error, because: ' .$pe->getmessage());     }      $sql = "select user_name users user_name = ':user_name' , password = ':password'";           $q = $conn->prepare($sql);      if(!$q){       die("execute query error, because: ". $conn->errorinfo());     };       $q->execute(array(':user_name'=>$user_name, ':password'=>$password));        $result = $q->fetchall();      if ($result['user_name']=$user_name){        $valid_user = $user_name;       $_session['valid_user'] = $valid_user;       return true;     }     else{         $text = "user name , password did not match";       write_log_in($text);     }   }   else {       $text = "this secure server. please log in.";     write_log_in($text);   } } 

as side note, i'm aware passwords should stored in @ least md5 hash format or similar. wanted working @ before adding in more stuff.

this problem

if ($result['user_name']=$user_name){  

it should

if ($result['user_name']==$user_name){  

but why don't check if have result determine if user correct. because pass user in sql query.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -