Getting fetch_assoc() error after implementing script

i bought a website one year ago. And now i created a real-time chat script with php it works well on test website. But when i try to upload on my main website i am gettin “Error: Call to a member function fetch_assoc()” error. Whats causes the error i couldnt find it or is there a way to fix it?

Chat has login and register inputs and its connected to my main database that created one year ago.
Database connecting successfully, so the problem is something else.
Here is my index.php code

<?php
require 'php/database.php';
session_start();

if(isset($_SESSION['user']) && $_SESSION['user'] == true) {
    $query = "INSERT INTO online_status (user_id, status) VALUES (".$_SESSION['userdata']['id'].", 1)";
    mysqli_query($db, $query);
}

if(empty($_GET['toUser'])) {
    header("Location: ?toUser=NULL");
    exit;
}

if(isset($_GET['toUser'])) {
    $toUserId = $_GET['toUser'];
    $select = "SELECT * FROM users WHERE id = $toUserId";
    $query = $db->query($select);

    if($query && $query->num_rows > 0) {
        $row = $query->fetch_assoc();
        // Rest of your code handling the query result
    } elseif($toUserId == "NULL") {
        // Handle case when toUser is explicitly set to NULL
    } else {
        header("Location: ?toUser=NULL");
        exit;
    }
}

if(isset($_POST['search_input'])) {
    $user = strip_tags($_POST['search_user']);
    $select = "SELECT * FROM users WHERE full_name = '$user'";
    $query = $db->query($select);

    if($query && $query->num_rows > 0) {
        $row = $query->fetch_assoc();
        // Rest of your code handling the query result
        header("Location: ?toUser=".$row['id']);
        exit;
    }
}
?>

Error log is

PHP Warning:  mysqli_fetch_array() expects parameter 1 to be mysqli_result, bool given in /home/xxxx/public_html/chatsystem/index.php on line 280
PHP Notice:  Trying to access array offset on value of type null in /home/xxxx/public_html/chatsystem/index.php on line 281

And here is the another error on db folder

PHP Warning:  mysqli_fetch_all() expects parameter 1 to be mysqli_result, bool given in /home/xxxx/public_html/chatsystem/php/function.php on line 70
PHP Warning:  count(): Parameter must be an array or an object that implements Countable in /home/xxxx/public_html/chatsystem/php/function.php on line 71

Also checked PHP modules and it seems correct to me. Changed mysqli to mysqlnd but problem still exists.