Code Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kingdavidbaker
Forum Newbie
Posts: 3
Joined: Mon Nov 30, 2009 1:16 pm

Code Help

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Code Help

Post by requinix »

= is assignment, == is comparison.
kingdavidbaker
Forum Newbie
Posts: 3
Joined: Mon Nov 30, 2009 1:16 pm

Re: Code Help

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Code Help

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kingdavidbaker
Forum Newbie
Posts: 3
Joined: Mon Nov 30, 2009 1:16 pm

Re: Code Help

Post 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
Post Reply