dtjo51649
2015-03-11 20:14
浏览 159
已采纳

PHP致命错误:调用未定义的方法

Here I import the files.class.php class which contains the getAllFiles() function:

include_once('lib/files.class.php');

Here I am calling the function :

<?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);
?>

This is the getAllFiles() function :

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

I am getting the following error :

Fatal error: Call to undefined method Files::getAllFiles() in /var/www/localhost/htdocs/mbax4cl3/groupproject/map.php on line 83

Thank you !

图片转代码服务由CSDN问答提供 功能建议

这里我导入包含getAllFiles()函数的files.class.php类: include_once('lib / files.class.php');

我在这里调用函数: \ n

 &lt;?php 
 $ files = new Files($ db); 
 $ jsonEncoding = json_encode($ files-&gt; getAllFiles()); 
 $ jsonFile = fopen(“  jsonEncoded.json“,”w“)或死(”无法打开文件!“); 
 fwrite($ jsonFile,$ jsonEncoding); 
 $ contentOfFile = file_get_contents('./ jsonEncoded.json'); 
  echo $ contentOfFile; 
 fclose($ jsonFile); 
?&gt; 
   
 
 

这是getAllFiles()函数:

  public function getAllFiles(){
 if(!($ stmt = $ this-&gt; connection-&gt; prepare(“SELECT ID,Latitude,Longitude,Name,Radius FROM Files”))){  
 $ this-&gt; lastError ='无法准备查询:('。$ this-&gt; connection-&gt; errno。')'。$ this-&gt; connection-&gt; error; 
 return false; \  n}其他{
  //执行查询并存储结果集
 $ stmt-&gt; execute(); 
 $ stmt-&gt; store_result(); 
 
 //将结果绑定到变量
 $ stmt-&gt;  bind_result($ id,$ latitude,$ longitude,$ name,$ radius); 
 $ results = array(); 
 //继续获取行
而($ stmt-&gt; fetch()){
  //添加到数组
 $ results [] = array(
'ID'=&gt;  $ id,
'纬度'=&gt;  $ latitude,
'经度'=&gt;  $经度,
'名称'=&gt;  $ name,
'Radius'=&gt;  $ radius,
); 
} 
 //返回结果数组
返回$ results; 
} 
} 
   
 
 

我得到了 以下错误:

致命错误:在/var/www/localhost/htdocs/mbax4cl3/groupproject/map.php上调用未定义的方法Files :: getAllFiles() 83

谢谢!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doz22551 2015-03-11 21:00
    已采纳

    Even if it doesn't make any sense , the problem seems to be with the code after this line : $jsonEncoding = json_encode($files->getAllFiles()); However, the error occurs on that line.

    I changed the code to the following and it works fine :

        <?php 
      $files = new Files($db);
      $jsonEncoding = json_encode($files->getAllFiles());
      print_r($jsonEncoding);
    ?>
    

    Thank you !

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题