0

I am using Ratchet in my php video Chat app and I am using my own class to access the users data in teh chat.php file of the Ratchet and it works fine but when i tray to var_dump the users data ratchet retunt "bool(false)" in the terminal here is my entire code

users.php

<?php
namespace MyApp;
use PDO;
class User{
    public $db,$userID,$sessionID;

    public function __construct(){
        $db = new \MyApp\DB;

        $this->db =  $db->connect();
        $this->userID = $this->ID();
        $this->sessionID = $this->getSessionID();
    }
    public function ID(){
        if($this->isLoggedIn()){
            return $_SESSION['userID'];
        }
    }
public function userData($userID=''){
        $userID = ((!empty($userID)) ? '$userID' : $this->userID);
        $stmt = $this->db->prepare("SELECT *FROM `users` WHERE `userID`=:userID ");
        $stmt->bindParam(":userID",$userID,PDO::PARAM_STR);
        //var_dump($userID);
        $stmt->execute();
         return $stmt->fetch(PDO::FETCH_OBJ);
    }
?>

chat.php

<?php
namespace MyApp;

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface
{
    protected $clients;
    public $userObj,$data;

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

    public function onOpen(ConnectionInterface $conn)
    {
        // Store the new connection to send messages to later
        $this->clients->attach($conn);
        var_dump($this->userObj->userData('2'));//this return false
        echo "New connection! ({$conn->resourceId})\n";
    }

    public function onMessage(ConnectionInterface $from, $msg)
    {
        $numRecv = count($this->clients) - 1;

        echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
            , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');

        foreach ($this->clients as $client) {
            if ($from !== $client) {
                // The sender is not the receiver, send to each client connected
                $client->send($msg);
            }
        }
    }

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

        echo "Connection {$conn->resourceId} has disconnected\n";
    }

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

        $conn->close();
    }
}

any one who can help me please?

kutakt
  • 3
  • 2
  • From https://www.php.net/manual/en/pdostatement.fetch.php: "*The return value of this function on success depends on the fetch type. In all cases, false is returned on failure or if there are no more rows.*" – Qirel Aug 06 '22 at 11:06
  • what dose that mean? it is not clear – kutakt Aug 06 '22 at 11:10
  • What's not clear about it? It explains right there why the return value can be false. – Qirel Aug 06 '22 at 13:37

0 Answers0