Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 调用未定义的方法;如何修复?_Php - Fatal编程技术网

Php 调用未定义的方法;如何修复?

Php 调用未定义的方法;如何修复?,php,Php,我在PHP中遇到以下致命错误: 致命错误:在第37行的C:\wamp\www\12.01.2015 Class 01\Coffee Website\Controller\CoffeeController.php中调用未定义的方法CoffeeModel::GetCoffeeByType() 上述错误的代码如下所示: function CreateCoffeeTables($types) { $coffeeModel = new CoffeeModel(); $coffeeArra

我在PHP中遇到以下致命错误:

致命错误:在第37行的C:\wamp\www\12.01.2015 Class 01\Coffee Website\Controller\CoffeeController.php中调用未定义的方法CoffeeModel::GetCoffeeByType()

上述错误的代码如下所示:

function CreateCoffeeTables($types)
{
    $coffeeModel = new CoffeeModel();

    $coffeeArray = $coffeeModel->GetCoffeeByType($types);
    $result = "";

    // Generate a coffeeTable for each coffeeEntity in array
    foreach ($coffeeArray as $key => $coffee) {
        $result = $result .
            "<table class = 'coffeeTable'>
            <tr>
            <th rowspan='6' width= '150px' ><img runat = 'server' src = '$coffee->image'/></th>
            <th width = '75px' >Name: </th>
            <td>$coffee->name</td>
            </tr>

            <tr>
            <th>Type: </th>
            <td>$coffee->type</td>
            </tr>

            <tr>
            <th>Price: </th>
            <td>$coffee->price</td>
            </tr>

            <tr>
            <th>Roast: </th>
            <td>$coffee->roast</td>
            </tr>

             <tr>
            <th>Origin: </th>
            <td>$coffee->country</td>
            </tr>

            <tr>
            <td colspan='2' >$coffee->review</td>
            </tr>

             </table>";
    }
    return $result;
}
函数CreateCoffeeTables($types)
{
$coffeeModel=新的coffeeModel();
$coffeeArray=$coffeeModel->GetCoffeeByType($types);
$result=“”;
//为数组中的每个coffeeEntity生成一个coffeeTable
foreach($coffeeArray作为$key=>$coffee){
$result=$result。
"
图片'/>
姓名:
$coffee->name
类型:
$coffee->type
价格:
$咖啡->价格
烤:
$咖啡->烤
来源:
$coffee->country
$coffee->review
";
}
返回$result;
}
在这段代码中,CoffeeModel类是存在的

<?php

require ("Entities/CoffeeEntity.php");

// contains database related code for the coffee type

class CoffeeModel
{

  //  Get all coffee types from the database and return them in an array

function GetCoffeeTypes()

{

 require 'Credentials.php';

 //Open connection and Select database

 mysql_connect($host, $user, $password) or die (mysql_error());

 mysql_select_db($database);

 $result = mysql_query("SELECT DISTINCT type FROM coffee") or die(mysql_error());

 $types = array();


 // Get data from databse

 while($row = mysql_fetch_array($result))

 {

    array_push($types, $row[0]);


 }

// Close connection and return

  mysql_close();

  return $types;  

}
}

// GET coffeeEntity objects from the database and return them in an array.

function GetCoffeeByType($type)

{

require 'Credentials.php';

// Open connection and select database

mysql_connect($host, $user, $password) or die (mysql_error());

 mysql_select_db($database);

 $query = "SELECT * FROM coffee WHERE LIKE '$type'";

 $result = mysql_query($query) or die (mysql_error());

 $coffeeArray = array();

 //GET Data from Database

 while ($row = mysql_fetch_array($result))

 {

    $name = $row[1];

    $type = $row[2];

    $price = $row[3];

    $roast = $row[4];

    $country = $row[5];

    $image = $row[6];

    $review = $row[7];


    // Create Coffee objects and store them in an array

    $coffee = new CoffeeEntity (-1, $name, $type, $price, $roast, $country, $image, $review);

    array_push($coffeeArray, $coffee);

 }

 // CLose connection and return result

 mysql_close();

 return $coffeeArray;


 }

GetCoffeeTypes()
方法之后,您似乎过早地关闭了您的类:

  return $types;  

}
}
^ here

// GET coffeeEntity objects from the database and return them in an array.

您需要将右大括号移动到文件末尾,因为现在您正在定义一个全局函数而不是类方法。

好的,只关注发布的问题,您的问题是在第一个方法之后关闭类:

}
} <---- this closing bracket is terminating the class

// GET coffeeEntity objects from the database and return them in an array.

function GetCoffeeByType($type)

{
}

}从错误消息中您不清楚什么?是的,在
CoffeeModel
类中创建一个
GetCoffeeByType()
方法。它告诉您该方法不存在。你能带我们去看咖啡模型课吗?另外,$result=$result。“xxx”;这是多余的。只需做$result.=“xxx”;我不能理解这个错误,在我看来一切都是正确的。我认为类或某物方法定义类型错误发生在这里是的,这是咖啡模型类移动括号后,这个错误发生了,您的SQL语法有错误;查看与MySQL服务器版本对应的手册,了解第1行“LIKE 4”附近使用的正确语法是什么,因为您的查询中缺少一些详细信息:
“SELECT*FROM coffee WHERE`column`LIKE'$type'”
-哪一列应该像$type?您应该切换到PDO或mysqli并使用准备好的语句,但现在您的问题只是查询语法错误。
}
} <---- this closing bracket is terminating the class

// GET coffeeEntity objects from the database and return them in an array.

function GetCoffeeByType($type)

{