phpJoeMo Posted March 16, 2011 Share Posted March 16, 2011 Hi all, I'm trying to clean up some code and eliminate redundancy. I'm trying to include a variable function inside of another variable function, but for some reason am having some difficulty. These functions dbDelete() is a function that opens a connection to MySQL. The user only has DELETE Privileages. I can ru dbDelete() by itself. My problem comes when I try to include dbDelete() as part of a global session cleanup/destroy function. That function's name is TimeOutAct(). Observe the following code: <?php $userID = array('scott'); function dbDelete($param){ $conDelete = mysql_connect($url,'myusername','mypassword',true); mysql_select_db('mydatabase',$conDelete); mysql_query($param,$conDelete); mysql_close(); if(isset($conDelete)){ mysql_close($conDelete); } } function timeOutAct(){ dbDelete("DELETE FROM acctussessi WHERE acctussessi_usid = '$userID[0]'"); $_SESSION = array(); setcookie(session_name(),'',time()-4200); session_destroy(); } //function dbDelete() isn't called when I do this :0 timeOutAct(); //if I do the following dbDelete() is called: dbDelete("DELETE FROM acctussessi WHERE acctussessi_usid = '$userID[0]'"); ?> Please help me. I a bit of a newb and need the expertise here ! Link to comment https://forums.phpfreaks.com/topic/230769-trouble-with-fuctions-please-help-me/ Share on other sites More sharing options...
kenrbnsn Posted March 16, 2011 Share Posted March 16, 2011 The array $userID is not defined inside the function timeOutAct. You need to pass it in as a parameter, just like you do in the function dbDelete: <?php $userID = array('scott'); function dbDelete($param){ $conDelete = mysql_connect($url,'myusername','mypassword',true); mysql_select_db('mydatabase',$conDelete); mysql_query($param,$conDelete); mysql_close(); if(isset($conDelete)){ mysql_close($conDelete); } } function timeOutAct($user){ dbDelete("DELETE FROM acctussessi WHERE acctussessi_usid = '$user'"); $_SESSION = array(); setcookie(session_name(),'',time()-4200); session_destroy(); } timeOutAct($userID[0]); ?> In the future when you post code to this forum, please put it between tags. Ken Link to comment https://forums.phpfreaks.com/topic/230769-trouble-with-fuctions-please-help-me/#findComment-1188042 Share on other sites More sharing options...
phpJoeMo Posted March 16, 2011 Author Share Posted March 16, 2011 Wow! Thanks a bunch Ken. You pointed me in the right direction. I researched variable scope further and now I understand what my problem was. Instead of passing a parameter, I used the global keyword inside of dbDelete() thus $userID = array('scott'); function dbDelete($param){ $conDelete = mysql_connect($url,'myusername','mypassword',true); mysql_select_db('mydatabase',$conDelete); mysql_query($param,$conDelete); mysql_close(); if(isset($conDelete)){ mysql_close($conDelete); } } function timeOutAct(){ global $userID; dbDelete("DELETE FROM acctussessi WHERE acctussessi_usid = '$userID[0]'"); $_SESSION = array(); setcookie(session_name(),'',time()-4200); session_destroy(); } //function dbDelete() is now called timeOutAct(); This way I can avoid excessive typing everytime I call the timeOutAct() procedure. For anyone else who may be experiencing the same thing I found the following resources to explain this issue further: http://php.net/manual/en/language.variables.scope.php http://www.elated.com/articles/php-variable-scope-all-you-need-to-know/ Thanks again Ken! :D :D Link to comment https://forums.phpfreaks.com/topic/230769-trouble-with-fuctions-please-help-me/#findComment-1188189 Share on other sites More sharing options...
kenrbnsn Posted March 16, 2011 Share Posted March 16, 2011 Don't use globals, pass the variables the function needs. Functions should be programmed as generic as possible, using a global in a function restricts where you can use it. Ken Link to comment https://forums.phpfreaks.com/topic/230769-trouble-with-fuctions-please-help-me/#findComment-1188201 Share on other sites More sharing options...
phpJoeMo Posted March 17, 2011 Author Share Posted March 17, 2011 I can understand that in most situations. Given the one I'm in don't you think its over redundant? I mean, the function is going to need the username everytime no matter what. Link to comment https://forums.phpfreaks.com/topic/230769-trouble-with-fuctions-please-help-me/#findComment-1188482 Share on other sites More sharing options...
kenrbnsn Posted March 17, 2011 Share Posted March 17, 2011 Please see this reply in another similar topic. It explains everything much better. Ken Link to comment https://forums.phpfreaks.com/topic/230769-trouble-with-fuctions-please-help-me/#findComment-1188677 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.