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
https://forums.phpfreaks.com/topic/230769-trouble-with-fuctions-please-help-me/
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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.