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.
Code Help
Moderator: General Moderators
Re: Code Help
= is assignment, == is comparison.
-
kingdavidbaker
- Forum Newbie
- Posts: 3
- Joined: Mon Nov 30, 2009 1:16 pm
Re: Code Help
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.
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.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Code Help
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:tasairis wrote:= is assignment, == is comparison.
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'];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
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
Thanks Again!
~David