PHP Fatal error: Call to undefined method
PHP Fatal error: Call to undefined method
这里我导入了包含 getAllFiles() 函数的 files.class.php class:
include_once('lib/files.class.php');
这里我调用函数:
<?php
$files = new Files($db);
$jsonEncoding = json_encode($files->getAllFiles());
$jsonFile = fopen("jsonEncoded.json", "w") or die ("Unable to open file!");
fwrite($jsonFile, $jsonEncoding);
$contentOfFile = file_get_contents('./jsonEncoded.json');
echo $contentOfFile;
fclose($jsonFile);
?>
这是 getAllFiles() 函数:
public function getAllFiles() {
if (!($stmt = $this->connection->prepare("SELECT ID, Latitude, Longitude, Name, Radius FROM Files"))) {
$this->lastError = 'Failed to prepare query: ('.$this->connection->errno.') '.$this->connection->error;
return false;
} else {
// Execute the query and store the result set
$stmt->execute();
$stmt->store_result();
// Bind the results to variables
$stmt->bind_result($id, $latitude, $longitude, $name, $radius);
$results = array();
// Keep fetching rows
while ($stmt->fetch()) {
// Add to array
$results[] = array(
'ID' => $id,
'Latitude' => $latitude,
'Longitude' => $longitude,
'Name' => $name,
'Radius' => $radius,
);
}
// Return the results array
return $results;
}
}
我收到以下错误:
致命错误:第 83 行 /var/www/localhost/htdocs/mbax4cl3/groupproject/map.php 中调用未定义的方法 Files::getAllFiles()
谢谢!
即使没有任何意义,问题似乎出在这一行之后的代码上:$jsonEncoding = json_encode($files->getAllFiles());
但是,错误发生在该行。
我将代码更改为以下代码并且工作正常:
<?php
$files = new Files($db);
$jsonEncoding = json_encode($files->getAllFiles());
print_r($jsonEncoding);
?>
谢谢!
这里我导入了包含 getAllFiles() 函数的 files.class.php class:
include_once('lib/files.class.php');
这里我调用函数:
<?php
$files = new Files($db);
$jsonEncoding = json_encode($files->getAllFiles());
$jsonFile = fopen("jsonEncoded.json", "w") or die ("Unable to open file!");
fwrite($jsonFile, $jsonEncoding);
$contentOfFile = file_get_contents('./jsonEncoded.json');
echo $contentOfFile;
fclose($jsonFile);
?>
这是 getAllFiles() 函数:
public function getAllFiles() {
if (!($stmt = $this->connection->prepare("SELECT ID, Latitude, Longitude, Name, Radius FROM Files"))) {
$this->lastError = 'Failed to prepare query: ('.$this->connection->errno.') '.$this->connection->error;
return false;
} else {
// Execute the query and store the result set
$stmt->execute();
$stmt->store_result();
// Bind the results to variables
$stmt->bind_result($id, $latitude, $longitude, $name, $radius);
$results = array();
// Keep fetching rows
while ($stmt->fetch()) {
// Add to array
$results[] = array(
'ID' => $id,
'Latitude' => $latitude,
'Longitude' => $longitude,
'Name' => $name,
'Radius' => $radius,
);
}
// Return the results array
return $results;
}
}
我收到以下错误:
致命错误:第 83 行 /var/www/localhost/htdocs/mbax4cl3/groupproject/map.php 中调用未定义的方法 Files::getAllFiles()
谢谢!
即使没有任何意义,问题似乎出在这一行之后的代码上:$jsonEncoding = json_encode($files->getAllFiles()); 但是,错误发生在该行。
我将代码更改为以下代码并且工作正常:
<?php
$files = new Files($db);
$jsonEncoding = json_encode($files->getAllFiles());
print_r($jsonEncoding);
?>
谢谢!