Page 1 of 1

Code Help

Posted: Mon Nov 30, 2009 1:19 pm
by kingdavidbaker
Hello all. I'm trying to code the simple php IF statement below but it does not work properly. Basically it always returns the accepted condition. Wondering if there is something wrong with my query or the IF statement itself. It seems the query statement always returns a "1" value but I need to pull the actual data. Basically I need a query to give me the exact data located in a field and then check that result with my If statement.

$status = mysql_query("SELECT status FROM qls3_users WHERE username = '$username';");

if($status="IN") {
header("location: phpinfo.php");
exit();
}else {
header("location: login-failed.php");
exit();
}

Dunno if this is a PHP or MySql issue but any help would be greatly appreciated. Also just in case someone knows the statement above is set to redirect to "phpinfo.php" if accepted. I don't want to redirect to another page, I simply want it to show the current page if accepted and redirect if denied. I am using this to help deny multiple logins to my site and want to include this in the header of every page.

Re: Code Help

Posted: Mon Nov 30, 2009 1:59 pm
by requinix
= is assignment, == is comparison.

Re: Code Help

Posted: Mon Nov 30, 2009 2:30 pm
by kingdavidbaker
Okay so I imagine my code should be

if($status=="IN") {
bla bla

That still does not work but i believe it is because my query is incorrect. If I print the

$status = mysql_query("SELECT status FROM qls3_users WHERE username = '$username';");

it comes up with "Resource ID #21" but I need it to show the actual data or word written in that field. I see alot of mysql fetch functions but they return entire rows as arrays and such. I just need the info from a single field and then have my IF statement compare against that.

Re: Code Help

Posted: Mon Nov 30, 2009 2:32 pm
by AbraCadaver
tasairis wrote:= is assignment, == is comparison.
Yes, and once you fix that you'll now never have the IF evaluate to true because $status is a resource. You need to do something like:

Code: Select all

$result = mysql_query("SELECT status FROM qls3_users WHERE username = '$username';");
 
list($status) = mysql_fetch_array($result);
//or
$row = mysql_fetch_array($result);
$status = $row['status'];
-Shawn

Re: Code Help

Posted: Mon Nov 30, 2009 2:53 pm
by kingdavidbaker
Thanks for the help. Yeah I fixed the issue using the mysql_result($status, 0); and that command gave me the data I wanted. I do see alot of people saying not to use the mysql_result command becaue the "fetch" commands work better and faster so I will play around with your solution to see if I can get it working.

Thanks Again!
~David