Trying to call method: undefined function error

余生颓废 提交于 2019-12-23 13:21:55

问题


I have a class to connect to my database, strip stuff and return things from a db query. Anyhow, the problem I am having is that I am trying to call runQuery() method but every time I try, I get this error:

Fatal error: Call to undefined function runQuery() in DatabaseConnector.php line 22

Any ideas perhaps? I know runQuery is private but it is within the same class. Just for kicks I changed it to public any way, and still got the same error :(

final class DatabaseConnector
{
    private $db;

    public function DatabaseConnector()
    {
        //  constructor
    }

    public function connectMySQL($host, $user, $passwrd, $db, $query)
    {
        @ $db = new mysqli($host, $user, $passwrd, $db);

        if (mysqli_connect_errno())
        {
            return mysqli_connect_errno();
        }
        else
        {
            $queryResult = runQuery($query);

            return $queryResult;
        }
    }

    private function runQuery($query)
    {
        $result = $db->query($query);

        return $result;
    }
}

回答1:


In PHP you have to prefix object level methods/variables with $this otherwise it will look for the function/variable in the global "namespace".

So change $queryResult = runQuery($query); to $queryResult = $this->runQuery($query);



来源:https://stackoverflow.com/questions/1149234/trying-to-call-method-undefined-function-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!