Jump to content

other mysql connection inside functions


zero_ZX

Recommended Posts

Hi,

So as you might (or might not) have guessed I need to open/close new connections inside my functions.

 

Here's my current code:

 

<?PHP

/**
* This function checks the ban status of the account.
* @return      1 if banned 
*/
function checkBan()
{
    mysql_close($con);
    require("./includes/wow.php");
    $result = mysql_query("SELECT * FROM wow_logon.accounts WHERE forum_acc= '.$user->data['user_id'].'");
    $row = mysql_fetch_array($result);
    if($row["banned"] == "1")
    {
        return 1;
        $ban_reason = $row["banreason"];
    }
    //$user->data['user_id']
     
    mysql_close($connect);        
    require("./includes/config.php");
}


?>

 

When trying to use the function:

 

checkBan();

 

if(checkban() == "1")

{

    echo'function works, and returns 1. Ban reason: '.$ban_reason.' ';

}

 

This gives a pretty good idea of what I try to accomplish I hope, if not, an explanation is below.

This code unfortunately returns:

[phpBB Debug] PHP Warning: in file /home/fusion/public_html/includes/functions_user.php on line 9: mysql_close() expects parameter 1 to be resource, null given
[phpBB Debug] PHP Warning: in file /home/fusion/public_html/includes/functions_user.php on line 12: mysql_fetch_array() expects parameter 1 to be resource, boolean given
[phpBB Debug] PHP Warning: in file /home/fusion/public_html/includes/functions_user.php on line 9: mysql_close() expects parameter 1 to be resource, null given
[phpBB Debug] PHP Warning: in file /home/fusion/public_html/includes/functions_user.php on line 12: mysql_fetch_array() expects parameter 1 to be resource, boolean given

 

Line 9 & 12:

mysql_close($con);

$row = mysql_fetch_array($result);

 

 

Explanation:

What I want to do is that I have a user account panel.

When the user log in, I want to call a function to check if the user is banned. If the user is banned then we have returned 1, and display some banned message, followed by the ban reason.

P.S. is it possible to use stored variables inside a function?

 

When I do this, I connect to another sql server, by first closing the existing connection (if any) and open a new one to execute my statements. Then close that connection and resume the old one.

Link to comment
Share on other sites

You can have multiple MySQL connections open at the same time.  See the mysql_connect and mysql_query pages in the manual

 

You use variables inside functions by passing them in.  See the "variable scope" chapter in the manual.

 

$conn1 = mysql_connect('server1.yourdomain.com', 'root', 'password');
$conn2 = mysql_connect('server2.yourdomain.com', 'root', 'password'); 

function checkBannedUsers( $userId, $conn1, $conn2 ) {
  $rs = mysql_query("SELECT * FROM users WHERE userId = '" . mysql_real_escape_string($userId) . "'", $conn1);
  $row = mysql_fetch_array($rs);
  if ( $row['banned'] == 1 ) {
      mysql_query("UPDATE bannedUsers SET loginAttempts = loginAttempts + 1 WHERE userId = '" . mysql_real_escape_string($userId) . "'", $conn2);
  }
}

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.