Function Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
VzR iRaTe
Forum Newbie
Posts: 3
Joined: Thu Jul 09, 2009 4:07 pm

Function Problem

Post by VzR iRaTe »

Alrighty, so I'm in process of updating the way my script displays errors...

Here's the function:

Code: Select all

 
function DisplayError($ERROR1){
    //Getting Information for Requested Error.
    $ERROR_QUERY = $hTbase->query("SELECT * FROM errors WHERE Error = '" . $ERROR1 . "'");
    $ERROR = $hTbase->fetch_array($ERROR_QUERY);
    $ERROR_TEXT = $ERROR['ErrorText'];
    //Set HTML style for Error.
    $ERROR_HTML_START = "<center><font color='red'>";
    $ERROR_HTML_END = "</font></center>"; 
    Return $ERROR_HTML_START . $ERROR_TEXT . $ERROR_HTML_END;
}
 
Alright, in MySQL the information is:
Table: "errors"
Identification for the error: "Error" Example: "MOD_NON_EXIST"
And the text for the error: "ErrorText" Example: "The mod you are currently requesting does not exist."

I'm calling the function like this:
let's say I'm doing a num_rows check for SQL.

Code: Select all

 
if($TeamNum >= "1"){ //If User is already on a team.
        DisplayError(MOD_NON_EXIST);
}
else{
        //Do Work then redirect to success page
}
 
Although, in the end. The function returns nothing. I'm not quite sure, I've also tried calling it this way:

Code: Select all

 
DisplayError('MOD_NON_EXIST');
 
And

Code: Select all

 
DisplayError("MOD_NON_EXIST");
 
I'm kinda out of ideas, and it's probably a stupid mistake on my part..
Last edited by VzR iRaTe on Tue Jul 21, 2009 11:32 pm, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Function Problem

Post by superdezign »

Try a print_r() on $hTbase inside if your function.

PHP's functions have their own local scope, like most of the modern programming languages. You never define $hTbase inside of your function. You may want to use a singleton design on the $hTbase object's class and get a copy of the instance at the start of the function.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Function Problem

Post by requinix »

DisplayError will return something, not print it out.

super's comment about scope and how $hTbase doesn't exist in the function is also spot on. Using the global keyword is a bad habit to get into, but it can be useful at times.
VzR iRaTe
Forum Newbie
Posts: 3
Joined: Thu Jul 09, 2009 4:07 pm

Re: Function Problem

Post by VzR iRaTe »

Ah, alrighty. I'll deffenetly look it over and make the changes. I really appreciate the input. As I said, just a stupid mistake on my part, lmfao.
Post Reply