cooldood Posted August 24, 2011 Share Posted August 24, 2011 Hello! I am trying to code one part of my functions list, but when I run the function to test all if, elseif, and else statements, it always goes to the else statement. So basically it tries to connect to MySQL, retrieve a username, and check to see if it is active, banned, or inactive. It is always saying it is active, even when I change it's status to inactive via PHPMyAdmin. Here is the code: <?php function checkActive() { $checkActiveQuery = mysql_query("SELECT * FROM users WHERE user=adf"); if ($checkActiveQuery == "INACTIVE"){ echo "Your account has not been activated. If you have not recieved, or have misplaced the activation email, please contact the administrator."; } elseif ($checkActiveQuery == "BANNED"){ echo "You have been banned from our social network. If you believe this is an error, please contact the administrator."; } else { echo "Your account is active."; } } ?> The username I am testing with is "adf" without quotes. But, when I change its status to INACTIVE, it still says it's still active, same with banned. How do I make it retrieve this data from MySQL and it function properly? It seems that it is unable to retrieve this data specifically. Thanks! Nick. Quote Link to comment https://forums.phpfreaks.com/topic/245557-what-am-i-doing-wrong-in-my-functions-file/ Share on other sites More sharing options...
voip03 Posted August 24, 2011 Share Posted August 24, 2011 Try using mysql_fetch_assoc() Quote Link to comment https://forums.phpfreaks.com/topic/245557-what-am-i-doing-wrong-in-my-functions-file/#findComment-1261226 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 try something like this: (change database field name first) <?php function checkActive() { $checkActiveQuery = mysql_query("SELECT * FROM `users` WHERE `user`= 'adf'"); $result = mysql_fetch_assoc($checkActiveQuery); $status = $result['field_name']; // SHOULD BE THE NAME OF THE DB FIELD THAT HOLDS THE VALUE 'BANNED', 'ACTIVE' or 'INACTIVE' if ($status == "INACTIVE"){ echo "Your account has not been activated. If you have not recieved, or have misplaced the activation email, please contact the administrator."; }elseif ($checkActiveQuery == "BANNED"){ echo "You have been banned from our social network. If you believe this is an error, please contact the administrator."; }else { echo "Your account is active."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245557-what-am-i-doing-wrong-in-my-functions-file/#findComment-1261236 Share on other sites More sharing options...
cooldood Posted August 24, 2011 Author Share Posted August 24, 2011 Hi. After trying the mysql_fetch_assoc() function, I always seem to get an error every time I use it. The error is as follows: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/OpenSocial/include/functions/login_functions.php on line 4. I'm trying everything, and nothing seems to fix this error. Do one of you have any suggestions? I even went to the extreme of using WebStyle's code to look to see if that would work, but I still cannot seem to fix that error when it comes to that PHP function. Are there any alternatives? Thanks! Nick. Quote Link to comment https://forums.phpfreaks.com/topic/245557-what-am-i-doing-wrong-in-my-functions-file/#findComment-1261343 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 please post the exact code you're using when you get that error. Quote Link to comment https://forums.phpfreaks.com/topic/245557-what-am-i-doing-wrong-in-my-functions-file/#findComment-1261345 Share on other sites More sharing options...
cooldood Posted August 24, 2011 Author Share Posted August 24, 2011 My code is as follows: (it is basically the same as yours) <?php function checkActive() { $checkActiveQuery = mysql_query("SELECT * FROM users WHERE user=adf"); $accountStatus = Result['user']; $Result = mysql_fetch_assoc($checkActiveQuery); if ($checkActiveQuery == "INACTIVE"){ echo "Your account has not been activated. If you have not recieved, or have misplaced the activation email, please contact the administrator."; } elseif ($checkActiveQuery == "BANNED"){ echo "You have been banned from our social network. If you believe this is an error, please contact the administrator."; } else { echo "Your account is active."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245557-what-am-i-doing-wrong-in-my-functions-file/#findComment-1261348 Share on other sites More sharing options...
cyberRobot Posted August 24, 2011 Share Posted August 24, 2011 You're using the $Result before it exists: <?php $accountStatus = Result['user']; $Result = mysql_fetch_assoc($checkActiveQuery); ?> ...plus you're missing a $...should be: <?php $Result = mysql_fetch_assoc($checkActiveQuery); $accountStatus = $Result['user']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/245557-what-am-i-doing-wrong-in-my-functions-file/#findComment-1261349 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 your code is NOT the same as mine. I said: $status = $result['field_name']; // SHOULD BE THE NAME OF THE DB FIELD THAT HOLDS THE VALUE 'BANNED', 'ACTIVE' or 'INACTIVE' and then you check that specific value: if ($status == "INACTIVE"){ Quote Link to comment https://forums.phpfreaks.com/topic/245557-what-am-i-doing-wrong-in-my-functions-file/#findComment-1261352 Share on other sites More sharing options...
cooldood Posted August 24, 2011 Author Share Posted August 24, 2011 I think I have figured it out. Thanks so much guys! Anyways can someone tell me why exactly it will say "Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /PATH"? It just doesn't make any sense to me. Thanks! Nick. Quote Link to comment https://forums.phpfreaks.com/topic/245557-what-am-i-doing-wrong-in-my-functions-file/#findComment-1261355 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 this variable holds the query: $checkActiveQuery = mysql_query("SELECT * FROM users WHERE user=adf"); this variable fetches the results from your query $Result = mysql_fetch_assoc($checkActiveQuery); this variable grabs a specific field from the returned array $accountStatus = Result['user']; this error means your query failed mysql_fetch_assoc() expects parameter 1 to be resource Quote Link to comment https://forums.phpfreaks.com/topic/245557-what-am-i-doing-wrong-in-my-functions-file/#findComment-1261358 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.