问题描述
我有一个类连接到我的数据库,剥离东西,并返回一个db查询的东西。无论如何,我所遇到的问题是,我试图调用 runQuery()
方法,但每次我尝试,我得到这个错误:
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:
有什么想法吗?我知道 runQuery
是私有的,但它在同一个类中。只是为了踢我把它改为公共的任何方式,仍然有同样的错误:(
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;
}
}
推荐答案
在PHP中,您可以使用 / code>,否则它将在全局命名空间中查找函数/变量。
In PHP you have to prefix object level methods/variables with $this
otherwise it will look for the function/variable in the global "namespace".
因此更改 $ queryResult = runQuery $ query);
到 $ queryResult = $ this-> runQuery($ query);
这篇关于试图调用method:undefined函数的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!