JasonDavis JasonDavis - 2 months ago 9
PHP Question

Assistance with a class method in PHP

I am trying to make a function I can pass an mysql query into and get the result back or an error, it should also check to see if the user running the code is an admin, if they are then it will get a session variable that holds a number count of the mysql queries ran on the page and then increment it by 1 number and then set it back to a session, also if the user is an admin it will show any error messages.

I know it is not the best code but does it appear like it should work? Any tips to improve too please.

You can see that I am running another method that gets a session variables value, I then set it to a variable in the current method, I then increment it's value by 1 number and then run the setter method to save it to a session again. I am not really sure If that is the correct way of doing that? DO I really need to run all those methods and save them all to a local variable like I am?

Also I am getting this error


Fatal error: Using $this when not in object context in C:\webserver\htdocs\project2\includes\classes\Database.class.php on line 37


Line 37 is this

$this->user_role = $session->get('user_role');


Method...

public static function query($sql)
{
global $session; // the $session Object

//get the session value to see if user is an admin
$this->user_role = $session->get('user_role');

//if user is admin, get query counter session value and +1 to it and reset it to a session variable.
if ($this->user_role >= 9){
$this->querie_counter = $session->get('querie_counter');
$this->querie_counter++; // add +1 to the number
$session->set('querie_counter',$this->querie_counter)
}

//run mysql query
$result = mysql_query($sql);

if (!$result) {
// If admin is viewing then we show the SQL code and the error returned
if($this->user_role >= 9){
$error = '<BR><center><font size="+1" face="arial" color="red">An Internal Error has occurred.<BR> The error has been recorded for review</font></center><br>';
$sql_formatted = highlight_string(stripslashes($sql), true);
$error .= '<b>The MySQL Syntax Used</b><br>' . $sql_formatted .
'<br><br><b>The MySQL Error Returned</b><br>' . mysql_error();
}
die($error);
}
return $result;
}

Answer

Hmm no one has read the error or?

**Fatal error: Using $this when not in object context in C:\webserver\htdocs\project2\includes\classes\Database.class.php on line 37**

ok you cant use $this in a static *function* you have to use self::users

public static function query($sql)
{
    global $session; // the $session Object

    //get the session value to see if user is an admin
    self::user_role = $session->get('user_role');

    //if user is admin, get query counter session value and +1 to it and reset it to a session variable.
    if (self::user_role >= 9){
        self::querie_counter = $session->get('querie_counter');
        self::querie_counter++; // add +1 to the number
        $session->set('querie_counter',$this->querie_counter)
    }

    //run mysql query
    $result = mysql_query($sql);

    if (!$result) {
        // If admin is viewing then we show the sql code and the error returned
        if(self::user_role >= 9){
            $error = '<BR><center><font size="+1" face="arial" color="red">An Internal Error has Occured.<BR> The error has been recorded for review</font></center><br>';
            $sql_formatted = highlight_string(stripslashes($sql), true);
            $error .= '<b>The MySQL Syntax Used</b><br>' . $sql_formatted .
            '<br><br><b>The MySQL Error Returned</b><br>' . mysql_error();
        }
        die($error);
    }
    return $result;
}