zero_ZX Posted October 11, 2011 Share Posted October 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/248910-other-mysql-connection-inside-functions/ Share on other sites More sharing options...
ManiacDan Posted October 11, 2011 Share Posted October 11, 2011 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); } } Quote Link to comment https://forums.phpfreaks.com/topic/248910-other-mysql-connection-inside-functions/#findComment-1278336 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.