# Uncaught Error

**URL:** <https://www.phphelp.com/t/uncaught-error/35164>\
**Category:** Uncategorized\
**Created:** [December 15, 2022, 3:00am UTC](https://www.phphelp.com/t/uncaught-error/35164 "2022-12-15T03:00:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dabya](https://www.phphelp.com/letter_avatar_proxy/v4/letter/d/ecae2f/32.png) [@dabya](https://www.phphelp.com/u/dabya)\
**Post date:** [December 15, 2022, 3:00am UTC](https://www.phphelp.com/t/uncaught-error/35164/1 "2022-12-15T03:00:42Z")

</div>

**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**

```auto
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);

    }
```

```auto
<?php

include 'Header.php';

$subjectobject = new Subjects();

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

        {

            $sid= $_GET['subID'];

           $subjectobject->initWithSID($sid);

    $subID = $subjectobject->getSubjectID();

        }
```

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [December 15, 2022, 4:38am UTC](https://www.phphelp.com/t/uncaught-error/35164/2 "2022-12-15T04:38:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [December 15, 2022, 5:29am UTC](https://www.phphelp.com/t/uncaught-error/35164/3 "2022-12-15T05:29:13Z")

</div>

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 -

```auto
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.
