SamDOD Posted October 21, 2008 Share Posted October 21, 2008 I am using PHP scripts that use multiple files. I am wanting to call a function on the main page that is located in an include page. I want this function to run above the HTML tags. i have code up there now and they run fine but i don't know the syntax for calling a function from another file. any help will be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
GKWelding Posted October 21, 2008 Share Posted October 21, 2008 the php file in which the function resides should be 'included' using the include or require code. from then on in you just call the function like you would if the function was included in the php file you're coding in. eg. if a function fFunction() is located in a file called includes.php to use this function in index.php you would add include "includes.php"; then to call the function you would just add fFunction(); Nothing special except the include bit... Quote Link to comment Share on other sites More sharing options...
SamDOD Posted October 21, 2008 Author Share Posted October 21, 2008 i have the includes for the documents and the function works when i call it on the file it is on.. but i need it called on the main page when i set it there all i get is blank page. the site does not continue. any thoughts? Quote Link to comment Share on other sites More sharing options...
johntp Posted October 21, 2008 Share Posted October 21, 2008 show us some code please. Quote Link to comment Share on other sites More sharing options...
SamDOD Posted October 21, 2008 Author Share Posted October 21, 2008 this is in the included file: function logLogin(){ $Susername2 = $this->dbuser; $Spassword2 = $this->dbpass; $Shostname2 = $this->dbhost; $Sdatabase2 = $this->dbase; $dbhandle2 = mysql_connect($Shostname2, $Susername2, $Spassword2) or die("Unable to connect to MySQL"); $selected2 = mysql_select_db($Sdatabase2,$dbhandle2) or die("Could not select Database"); $SQL3="INSERT INTO userlog4(userName, lastLog, ip) "; $SQL3.="(SELECT userName, lastLog, ip "; $SQL3.="FROM myuser WHERE ".$this->tblID."='".$_SESSION['userID']."')"; $Send=mysql_query($SQL3) or die("INSERT ERROR: ".mysql_error()); mysql_close($dbhandle2); } this is at the top of the main page. <?php include("adminpro_config.php"); include("adminpro_class.php"); include("adminuser_config.php"); logLogin(); ?> Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 21, 2008 Share Posted October 21, 2008 The problem is in these lines: <?php $Susername2 = $this->dbuser; $Spassword2 = $this->dbpass; $Shostname2 = $this->dbhost; $Sdatabase2 = $this->dbase; $this refers to the current object (class) that you are working in. It cannot be called from outside a class, which you are doing. I assume that you have instantiated a class somewhere ($myvar = new classname()), most likely some database class. You need to pass that object to your function logLogin(). And then within logLogin() you need to use the reference ($myvar) instead of $this. so somewhere you are doing something like this: $db = new DB(); when you call your function then you need to do this: logLogin($db) and also change your logLogin() function to this: $Susername2 = $db->dbuser; $Spassword2 = $db->dbpass; $Shostname2 = $db->dbhost; $Sdatabase2 = $db->dbase; The names will all be different ($db) and will be whatever you set earlier in your code which wasn't posted, so I can't give specific advise. Quote Link to comment Share on other sites More sharing options...
SamDOD Posted October 22, 2008 Author Share Posted October 22, 2008 Thank you, it works now Quote Link to comment 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.