简体   繁体   中英

php execution quits at specific line

I am making a simple website. My site quits when I create a reference to another class.

$get_car_list = new GetCarList();
$car_list = $get_car_list->getEntireInventory();

It quits immediately upon the debugger hitting $get_car_list = new GetCarList(); .

The code for the class I am referencing is as follows.

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;

    }

}

I am really new to php so it could be something extremely simple. But I found it a little strange because the debugger and execution completely quits upon a certain line of code.

EDIT: Here is the log printout from the event

[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

I was doing

include '../model/file.php';

when I should have done

include 'model/file.php';

For some reason my IDE (PhpStorm) was telling me the file did not exist. It is still throwing a warning for some odd reason.

1st of all turn on all errors in :

error_reporting(E_ALL);
ini_set('display_errors', 1); // check your php.ini
$get_car_list = new GetCarList();
$car_list = $get_car_list->getEntireInventory();
  1. Do you have include somewhere before you call new GetCarList(); ? Or do you have class declaration before this line?

  2. Try to go step by step. So First iteration is to create one file with following code:

 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; 

So if it works you can start manipulate with your method getEntireInventory() which is bad so try to (but remember you are trying to create Car here - I have no idea where did you declare that class?):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM