使用PHP中的类方法提供帮助

时间:2010-01-16 22:23:25

标签: php

我正在尝试创建一个函数,我可以将mysql查询传递回来并获得结果或错误,它还应该检查运行代码的用户是否是管理员,如果他们是,那么它将得到一个会话变量,其中包含在页面上运行的mysql查询的数量计数,然后将其递增1个数字,然后将其设置回会话,如果用户是管理员,它将显示任何错误消息。

我知道这不是最好的代码,但它看起来应该有效吗?任何改进的提示也请。

你可以看到我正在运行另一个获取会话变量值的方法,然后我将它设置为当前方法中的变量,然后我将它的值增加1个数,然后运行setter方法将其保存到会议再次。我不确定如果这是正确的方法吗?我真的需要运行所有这些方法并将它们全部保存到像我这样的局部变量吗?

我也收到此错误

  

致命错误:在第37行的C:\ webserver \ htdocs \ project2 \ includes \ classes \ Database.class.php中不在对象上下文中时使用$ this

第37行是

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

...方法

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;
}

3 个答案:

答案 0 :(得分:3)

你不能在静态方法中使用$ this。静态方法不依赖于对象的任何实例,因此$这在静态方法中没有意义。

user_role也是一个静态变量吗?如果是这样,您需要使用Database :: $ user_role。

您还会在$ this-&gt; querie_counter。

中收到此错误

答案 1 :(得分:1)

嗯没有人读过这个错误或者?

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

好的,你不能在静态 功能中使用$this,你必须使用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;
}

答案 2 :(得分:-1)

似乎没问题,只需纠正以下几点:

  • 查询不是 querie
  • 您应该使用htmlspecialchars()代替stripslashes()
  • highlight_string()只突出显示PHP代码,而不是SQL - 它在那里做什么?
  • 在您的错误消息中写入有效的XHTML代码(无<BR><br> =&gt; <br />
相关问题