Uncaught Error

Fatal error: Uncaught TypeError: mysqli_fetch_object(): Argument #1 ($result) must be of type mysqli_result, bool given in C:\Users\dabya\OneDrive\Documents\xamp\htdocs\projectV2\Database.php:51 Stack trace: #0 C:\Users\dabya\OneDrive\Documents\xamp\htdocs\projectV2\Database.php(51): mysqli_fetch_object(false) #1 C:\Users\dabya\OneDrive\Documents\xamp\htdocs\projectV2\classes.php(873): Database->singleFetch(‘SELECT * FROM P…’) #2 C:\Users\dabya\OneDrive\Documents\xamp\htdocs\projectV2\manageSubject.php(7): Subjects->initWithSID(‘BU6001’) #3 {main} thrown in C:\Users\dabya\OneDrive\Documents\xamp\htdocs\projectV2\Database.php on line 51

function initWithSID($subjectID)

    {

        $db = Database::getInstance();

        $data = $db->singleFetch('SELECT * FROM PLMS_subjects where subjectID = ' . $subjectID);

        $this->initWith($data->subjectID, $data->subjectName, $data->MajorID, $data->CourseID);

    }
<?php

include 'Header.php';

$subjectobject = new Subjects();

if (isset($_GET['subID']))

        {

            $sid= $_GET['subID'];

           $subjectobject->initWithSID($sid);

    $subID = $subjectobject->getSubjectID();

        }

You can put bbcode [code][/code] tags, on their own lines, before and after your code, to format it. I have made this edit to your recent posts.

The error you are getting is a follow-on error, it is not the actual problem. The error is occurring because the sql query failed and there’s no error handling in this code to stop the code from continuing to run and try to use the result from a failed query.

You always need error handling for statements that can fail. For database statements that can fail - connection, query, prepare, and execute, the simplest way of adding error handling, without adding logic at each statement, is to use exceptions for errors and in most cases simply let php catch and handle the exception where php will use its error related settings to control what happens with the actual error information (database statement errors will ‘automatically’ get displayed/logged the same as php errors.)

To enable exceptions for errors for the mysqli extension, add the following line of code before the point where you make the database connection -

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

You will be getting an error about a non-existent column with the same name as the data value, BU6001. This is because that data value is a string and must be enclosed by single-quotes in the query. But don’t even do that. You should using a prepared query when supplying external, unknown, dynamic values to a query when it gets executed.

Sponsor our Newsletter | Privacy Policy | Terms of Service