在php数据库连接中使用两个SELECT查询

here is my_sqli database connection

class FbChatMock {
    // Holds the database connection
    private $dbConnection;
    private $_dbHost = 'localhost';
    private $_dbUsername = 'root';
    private $_dbPassword = '';
    public $_databaseName = 'erp5_temp2';
    public function __construct() {
        $this->dbConnection = new mysqli($this->_dbHost, $this->_dbUsername, 
            $this->_dbPassword, $this->_databaseName); 
        if ($this->dbConnection->connect_error) {
            die('Connection error.');
        }
    }

i am getting two parameters in the getchat function:

public function getchat($userId, $id){ 
    $meesage = array();

    $query = "SELECT u.user_id FROM  `users` u where u.id='$id'";
    $resultObj = $this->dbConnection->query($query);
    $user = $resultObj->fetch_assoc())
    $userid = $user;
}

I am getting the $userid variable from the query and using it inside this query:

$query = "SELECT u.id,c.message,c.sent_on FROM `chat` c JOIN 
    `users` u ON c.user_id=u.user_id where u.id='$id' AND c.user_id='$userid'";
     // Execute the query
     $resultObj = $this->dbConnection->query($query);
     // Fetch all the rows at once.
     while ($rows = $resultObj->fetch_assoc()){
         $meesage[] = $rows;
     }
     return $meesage;

And the problem is that my first sql query is not working correctly . i have tested it by showing $userid value from echo.

Chage this line

$userid = $user;

To this

$userid = $user['user_id'];

Because $user it is array with all columns you have selected, and you need get only ID from it.

Your problem lies here :

$user = $resultObj->fetch_assoc())
$userid = $user;

because mysqli::fetch_assoc() with retrieve an associative array. Thus you have to access it like so (given the column name in your select):

$userid = $user["user_id"];