Jump to content

What am I doing wrong in my functions file?


cooldood

Recommended Posts

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.

Link to comment
Share on other sites

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.";
}
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.";
}
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.