Jump to content

Trouble WITH fucTions //PLEASE HELP ME!!!!?? :)


phpJoeMo

Recommended Posts

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

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

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 :D :D :D

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.