So if you wanted to use an SQL if|else statement, look at something like Using an IF Statement in a MySQL SELECT query, or If else on WHERE clause.
Some other sql answers here look interesting too. However i would caution against overcomplicating.
For this OP its easier to prepare statement you send to DB, rather than overthink it and risk performance drop if massive dataset and poor indexes.
So, if this is more relevant as a php if|else, and you want to keep it as a single line (as opposed to concatenating a string over multiple statements), i would recommend ternary syntax
https://davidwalsh.name/php-shorthand-if-else-ternary-operators
For example, something like this should work:
$reuslt= $this->db-query("SELECT * FROM mytable WHERE TRUE "
. ( ($cid is not null) ? " AND cid='".$cid."'" : "" )
. ( ($room_id != null) ? " AND room_id='".$room_id."'" : "" )
. ( ($time != null) ? " AND time='" . $time . "'" : "" );
The WHERE TRUE is just to make easier to print, though it really would be easier to just create a $sql string variable and prepare the statement over seperate if() statements.
Though i would also stress the need for escaping these values, as you should with all user input.
http://php.net/manual/en/mysqli.real-escape-string.php