ThingEngineer / PHP-MySQLi-Database-Class

Wrapper for a PHP MySQL class, which utilizes MySQLi and prepared statements.
Other
3.29k stars 1.35k forks source link

Class mysqli does not exist #961

Closed CherukuriSiva closed 3 years ago

CherukuriSiva commented 3 years ago

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) {
    }
}`
CherukuriSiva commented 3 years ago

The issue is an extension not enabled inside the php.ini file. After uncommenting mysqli, found no issues.