我正在制作一个简单的网站。当我创建对另一个类的引用时,我的站点退出。
$get_car_list = new GetCarList();
$car_list = $get_car_list->getEntireInventory();
在调试器点击$get_car_list = new GetCarList();
时立即退出。
我引用的类的代码如下。
class GetCarList {
public function getEntireInventory() {
$car_list = array();
// Connecting, selecting database
$connection = mysqli_connect('localhost:8889', 'root', 'root')
or die('Could not connect: ' . mysqli_connect_error());
$queryStatementForIdList = "SELECT id FROM cars";
$r = mysqli_query($connection, $queryStatementForIdList);
$idList = mysqli_fetch_array($r);
for($i = 1; $i < sizeof($idList); $i++) {
$queryStatementForSingleCar = "SELECT * FROM cars WHERE id = " . $idList[$i];
$b = mysqli_query($connection, $queryStatementForSingleCar);
$carInfo = mysqli_fetch_assoc($b);
$thisCar = new Car($carInfo['id'], $carInfo['make'], $carInfo['model'], $carInfo['year'], $carInfo['thumbnail'],
$carInfo['photos'], $carInfo['miles'], $carInfo['price']);
array_push($car_list, $thisCar);
}
return $car_list;
}
}
我是php的新手,所以它可能非常简单。但我发现它有点奇怪,因为调试器和执行完全退出了某行代码。
编辑:这是事件的日志打印输出
[25-Feb-2015 04:28:27 Europe/Berlin] PHP Warning: include(../model/GetCarList.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/dushanemotors/controller/InventoryController.php on line 12
[25-Feb-2015 04:28:27 Europe/Berlin] PHP Stack trace:
[25-Feb-2015 04:28:27 Europe/Berlin] PHP 1. {main}() /Applications/MAMP/htdocs/dushanemotors/inventory.php:0
[25-Feb-2015 04:28:27 Europe/Berlin] PHP 2. InventoryController->__construct($url = *uninitialized*) /Applications/MAMP/htdocs/dushanemotors/inventory.php:11
[25-Feb-2015 04:28:27 Europe/Berlin] PHP Warning: include(): Failed opening '../model/GetCarList.php' for inclusion (include_path='.:/Applications/MAMP/bin/php/php5.6.2/lib/php') in /Applications/MAMP/htdocs/dushanemotors/controller/InventoryController.php on line 12
[25-Feb-2015 04:28:27 Europe/Berlin] PHP Stack trace:
[25-Feb-2015 04:28:27 Europe/Berlin] PHP 1. {main}() /Applications/MAMP/htdocs/dushanemotors/inventory.php:0
[25-Feb-2015 04:28:27 Europe/Berlin] PHP 2. InventoryController->__construct($url = *uninitialized*) /Applications/MAMP/htdocs/dushanemotors/inventory.php:11
答案 0 :(得分:0)
我在做
include '../model/file.php';
当我应该做的时候
include 'model/file.php';
出于某种原因,我的IDE(PhpStorm)告诉我该文件不存在。由于一些奇怪的原因,它仍在警告。
答案 1 :(得分:0)
首先打开所有错误:
error_reporting(E_ALL);
ini_set('display_errors', 1); // check your php.ini
$get_car_list = new GetCarList();
$car_list = $get_car_list->getEntireInventory();
在致电include
之前,您在某处有new GetCarList();
吗?
或者你在这行之前有课堂申报吗?
尝试一步一步走。因此,第一次迭代是使用以下代码创建一个文件:
error_reporting(E_ALL); ini_set('display_errors', 1); // check your php.ini class GetCarList { public function getEntireInventory() { return 'GetCarList Response'; } } $get_car_list = new GetCarList(); $car_list = $get_car_list->getEntireInventory(); echo $car_list;
所以,如果它有效,你可以用你的方法getEntireInventory()
开始操作,这是不好的,所以尝试(但记住你试图在这里创建Car
- 我不知道你在哪里声明该类?):
public function getEntireInventory() {
$car_list = array();
$connection = mysqli_connect('localhost:8889', 'root', 'root')
or die('Could not connect: ' . mysqli_connect_error());
$query = "SELECT * FROM cars ";
$result = mysqli_query($connection, $query);
while ($carInfo = mysqli_fetch_assoc($result)) {
$thisCar = new Car($carInfo['id'], $carInfo['make'], $carInfo['model'], $carInfo['year'], $carInfo['thumbnail'],
$carInfo['photos'], $carInfo['miles'], $carInfo['price']);
array_push($car_list, $thisCar);
}
return $car_list;
}