Jump to content

Archived

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

leeming

function defined, but isnt detected? (inbedded in a require?)

Recommended Posts

basicly i have my index page..

[code]
//call const
require_once("wwc_const.php");

//call functions
require_once("wwc_functions.php");[/code]

which the const page, calls a function, which gets the id of the user from their login name...

not sure if this is due to if the functions file is set as require_once (but i tried them both as include and same problem)...

[quote]
Fatal error: Call to undefined function: getidfromuser() in /home2/admin/public_html/wwc_const.php on line 8[/quote]

wwc_const.php  line #8
[code]  DEFINE("USERID", getIDFromUser($_SESSION[wwc_user]));[/code]

wwc_functions.php function
[code]function getIDFromUser($user)
{
  $sql = "select Account_ID from Accounts WHERE Account_UName = '$user'";
  $query = mysql_query($sql, cdb(0))or bavaderror($sql);
  $result = mysql_fetch_array($query);
 
  return $result[Account_ID];
}[/code]

Share this post


Link to post
Share on other sites
Are you calling the function previous to the require_once statement?

Share this post


Link to post
Share on other sites
[quote author=businessman332211 link=topic=105663.msg422171#msg422171 date=1156556560]
Try just an include, see if that works.
[/quote]
i did say i had tried that :S
[quote author=leeming link=topic=105663.msg422166#msg422166 date=1156555783]

not sure if this is due to if the functions file is set as require_once (but i tried them both as include and same problem)...
[/quote]


i am calling the function (in line order) before its defined, but functions can be declared at the bottom of the page arnt they... :S

Share this post


Link to post
Share on other sites
[quote] but functions can be declared at the bottom of the page arnt they... :S[/quote]
functions HAVE to be declared before the function is called.
As for your problem, just as a test try declaring the function on the same page, then see if it works, just try declaring it at the top of the page.  See if you get the same error.  If not then try
the function_exists() function, see if it returns true.
That will let you know if the function even exists, if it returns false when it's on the same page then something is wrong with your function itself.

Share this post


Link to post
Share on other sites
[quote]
functions HAVE to be declared before the function is called.
[/quote]

really? i was under the understanding that aslong as they are declared on the code, they can be called... (same understanding for other programming languages)

i can put the functions on my main index page.. instead of in a functions page.. but i have never had this problem before.. (it also makes my index.php page more scruffy looking)

if i declare the function above or below on the same page it still works.. (just since i have 3 custom functions called in the 'const' page.. which 2 of them (bavaderror() and cdb() are lengthy declarations), so if i put these on the main index page.. it defeats the purpose of having a functions.php page (???)...

like i said.. i have never had this problem before.. if i cant fix it (without resorting to making my code look scruffy and disordered) i will have to be forced to rearange my code

Share this post


Link to post
Share on other sites
You can keep all your functions in a seperate file as is what I do.. Just put your require statement before you are going to call the function..  Thats all that needs to happen..  If its going to break your code -- you might re-think your coding style.

Share this post


Link to post
Share on other sites
[quote]functions HAVE to be declared before the function is called.[/quote]

That is INCORRECT. You can use a function before it is declared in php. However... what you are trying to do is use a function before the file it is within is included. This will not work.

Share this post


Link to post
Share on other sites
just switch the order of these two and it should work

require_once("wwc_const.php");
require_once("wwc_functions.php");

just require the funcitons.php first and then const.php

Share this post


Link to post
Share on other sites

×

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.