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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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.

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.