Jump to content

[SOLVED] Calling a function fron another file


SamDOD

Recommended Posts

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.

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

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();

?>

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.

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.