mysql - PHP If / else user exists on database set variable to 0 or 1 -
i have code check if user exists in database. want assign $account
1
if exits, , 0
if doesn't. thought code work setting $account
0
think im missing trick. appreciated.
$con = mysql_connect(db_host, db_user, db_pass) or die("couldn't make connection."); if (!$con) { die('could not connect: ' . mysql_error()); } mysql_select_db(db_name, $con); $sql="select user_name users"; $result = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_assoc($result); if ($row['user_name'] == '$user_name'){ $account = 1; }else{ $account = 0;}
thanks
you can't put variables in string if use single quotes '
(plus it's not necessary!). below rectifies issue, , shortens if/else statement one-liner.
$account = $row['user_name']==$user_name ? 1 : 0;
Comments
Post a Comment