Is is possible to insert/update chat data to the mysql database in chat.php?
I am new at Ratchet Websocket and currently working on a simple chat application by using Ratchet Websocket library. The savemessage function is in the chat.php. 
After php models/server.php command is successfully executed, (i put my chat.php and server.php in models file --> https://imgur.com/a/fMuSGRR ) there is an error in line 84. I tried to call functions in the database.php or other classes, but the same error is shown as well. 

**Line 84:**
```php
$this->statement = $this->db_handler->prepare($query);
```

**Chat.php** 

```php
<?php
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
require_once dirname(__DIR__) . '/core/database.php';

class Chat implements MessageComponentInterface
{
    private $db;
    private $db_handler;
    private $statement;
    protected $clients;

    public function __construct()
    {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn)
    {
        // Store the new connection to send messages to later
        $this->clients->attach($conn);
        echo "Server Started\n";
        echo "New connection!\n";
    }

    public function onMessage(ConnectionInterface $from, $msg)
    { /*this function will receive data from the chat form */
        /* $msg = users messages */
        $numRecv = count($this->clients) - 1;
        echo "Sending message to other connection\n";
        $data = json_decode($msg, true);
        
        if ($this->savemessage('test')) {
            echo "Saved message to DB\n";
        } else {
            echo "Failed to save message\n";
        }
        
        $data['status_type'] = 'Online';
        $data['dt'] = date("d-m-Y h:i:s");
        foreach ($this->clients as $client) {
            /*
            if ($from !== $client) {
                // The sender is not the receiver, send to each client connected
                $client->send($msg);
            }*/
            if ($client !== $from) {
                $client->send(json_encode($data));
            }
        }
    }

    public function onClose(ConnectionInterface $conn)
    {
        // The connection is closed, remove it, as we can no longer send it messages
        $this->clients->detach($conn);

        echo "A user has disconnected\n";
    }

    public function onError(ConnectionInterface $conn, \Exception $e)
    {
        echo "An error has occurred: {$e->getMessage()}\n";

        $conn->close();
    }

    public function savemessage($data)
    {
        /*$this->db = Database::getInstance();*/
        $this->connect = 'mysql:host=' . $this->db_host . ';dbname=' . $this->db_name;
        $options = array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );
        try {
            $this->db_handler = new PDO($this->connect, $this->db_user, $this->db_pass, $options);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
            echo $this->error;
        }
        $query = "INSERT INTO chatrooms (id, uuid, msg, created_on) VALUES (default, :uuid, :msg, :created_on)";
        $this->statement = $this->db_handler->prepare($query);
        $type = PDO::PARAM_STR;
        $this->statement->bindValue(':uuid', $data['uuid'],$type);
        $this->statement->bindValue(':msg', $data['msg'],$type);
        $this->statement->bindValue(':created_on', $data['created_on'],$type);
        return ($this->statement->execute());
    }
}
```


After some google search, I believed that it is $pdo variable is not initialized (maybe?). Therefore. I tried to create a getinstance method in database.php. No luck. Still same error as well.

**Database.php get instance**
```php
public static function getInstance() {
        if (!self::$instance) {
            self::$instance = new self;
        }
        return self::$instance;
    }
```
Use the functions in database.php, for example:
```php
$this->db = Database::getInstance();
$query = "INSERT INTO chatrooms (id, uuid, msg, created_on) VALUES (default, :uuid, :msg, :created_on)";
$this->db->query($query);
$this->db->bind(':uuid', $data['uuid']);
$this->db->bind(':msg', $data['msg']);
$this->db->bind(':created_on', time());
$this->db->execute();
```
It would be greatly appreciated if anyone have any ideas about what the problem might be or how to fix it. Thanks! :confused: :confused: :confused: :anguished: :anguished: :anguished:

This is the error log. https://imgur.com/a/ktEevnp

