Riandy
Riandy

Reputation: 23

Displaying function result in other file

I've got two files: function.php and cobafunction.php.

I have code in function.php as follows:

function pegawai (){
$getpegawai = mysql_query
("select * from pegawai where nip ='005970458'");
$result = mysql_fetch_array ($getpegawai);

echo "$result[nip]";

}

How do I make echo "$result[nip]"; be displayed in another file that is in cobafunction.php?

Thank you.

Upvotes: 0

Views: 43

Answers (4)

ferry
ferry

Reputation: 64

You can use php static function like this

cobafunction.php

class yourClassName {
    static function pegawai (){
        $getpegawai = mysql_query("select * from pegawai where nip ='005970458'");
        $result = mysql_fetch_array($getpegawai);
        return $result['nip'];
    }
}

another file

echo yourClassName::pegawai();

Try it !

Upvotes: 0

weevil
weevil

Reputation: 56

At the end of the pegawai function, instead of:

echo "$result[nip]";

I would do:

return($result[nip]);

In cobafunction.php

include("function.php");

$var = pegawai();
print $var;

Upvotes: 0

Jason Kennaly
Jason Kennaly

Reputation: 652

You can call pegawai() from cobafunction.php

Example:

//cobafunction.php
<?php
require_once("function.php")
pegawai()
?>

Upvotes: 0

William Fernandes
William Fernandes

Reputation: 239

Your code should be something like:

function pegawai() {
    $getpegawai = mysql_query("select * from pegawai where nip ='005970458'");
    $result = mysql_fetch_array ($getpegawai);

    return $result[nip];
}

$someVariable = pegawai();

And you should include the function.php on the cobafunction.php file.

Upvotes: 1

Related Questions