Skip to content

Class mysqli does not exist #961

@CherukuriSiva

Description

@CherukuriSiva

I am trying to connect to MySQL DB using this library https://github.com/ThingEngineer/PHP-MySQLi-Database-Class inside the onMessage function.

I am receiving "User info" whenever the User sends a message inside onMessage() callback. I want to use this user info to fetch some details from MySQL DB. However, I am always receiving Class mysqli does not exist error. How can we connect to MySQL?

This line not throwing any error
$db = new MysqliDb(Config::DB_HOST, Config::DB_USER, Config::DB_PASSWORD, Config::DB_NAME);
But this line throwing error:
$numOfUsersInBet = $db->getValue("bet_users_info", "count(*)");

`<?php

namespace App;

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use MysqliDb;

class Socket implements MessageComponentInterface {
    protected $clients;

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

    public function onOpen(ConnectionInterface $conn) {

        // Store the new connection in $this->clients
        $this->clients->attach($conn);

        echo "New connection! ({$conn->resourceId})\n";
    }

    public function onMessage(ConnectionInterface $from, $msg) {

        $db = new MysqliDb(Config::DB_HOST, Config::DB_USER, Config::DB_PASSWORD, Config::DB_NAME);
        try {
            if ($db === null) {
                echo "DB Null \n";
            } else {
                $numOfUsersInBet = $db->getValue("bet_users_info", "count(*)");
                echo $numOfUsersInBet;
            }

        } catch (\Exception $e) {
            echo $e->getMessage();
        }

        foreach ( $this->clients as $client ) {

            if ( $from->resourceId == $client->resourceId ) {
                continue;
            }

            $obj = json_decode($msg, false);
           $client->send( "<b style='color: darkgreen;'>$obj->user :</b> $obj->message" );
        }
    }

    public function onClose(ConnectionInterface $conn) {
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
    }
}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions