我在PHP中遇到以下致命错误:
致命错误:在第37行的C:\ wamp \ www \ 12.01.2015 Class 01 \ Coffee Website \ Controller \ CoffeeController.php中调用未定义的方法CoffeeModel :: GetCoffeeByType()
Call Stack
# Time Memory Function Location
1 0.0030 142264 {main}( ) ..\Coffee.php:0
2 0.0130 162800 CoffeeController->CreateCoffeeTables( ) ..\Coffee.php:15
以上错误的代码如下所示:
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;
}
在此代码中,存在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;
}
我该如何解决这个问题?
答案 0 :(得分:1)
在GetCoffeeTypes()
方法之后,您似乎过早关闭了课程:
return $types;
}
}
^ here
// GET coffeeEntity objects from the database and return them in an array.
您需要将该右括号移动到文件的末尾,因为现在您要定义一个全局函数而不是类方法。
答案 1 :(得分:1)
好的,只关注发布的问题,你的问题是你在第一个方法之后关闭了课程:
}
} <---- this closing bracket is terminating the class
// GET coffeeEntity objects from the database and return them in an array.
function GetCoffeeByType($type)
{
只需将一个括号移动到文件的末尾即可。接下来就是了解班级组织,但这是另一个故事。