title: Run one of two different SELECT queries based on condition
body: <p>Here is my problem, in a nutshell.</p>
  
  <p>I have one table.  It contains messages I want to display based on the values of a start time column and an end time column relative to server time($time).</p>
  
  <p>the first row of the table is a default message that I want to display if there are NO messages with a start time &lt;= $time and end time > $time.  </p>
  
  <p>I can pull the most recent message and display it no problem using this query:</p>
  
  <pre><code>SELECT * FROM $table WHERE start &lt;= $time and end &gt; $time;
  </code></pre>
  
  <p>I can't figure out how to get my default to display if no other rows match the above criteria.  </p>
  
  <p>I tried this: </p>
  
  <pre><code> $sql="SELECT CASE WHEN (SELECT COUNT(*) FROM $table WHERE start &lt;= $time AND end &gt; $time) &gt; 0
      THEN (SELECT * FROM $table WHERE start &lt;= $time AND end &gt; $time)
      ELSE (SELECT * FROM $table WHERE id = 1)
      END";
  </code></pre>
  
  <p>but it returns this error:</p>
  
  <pre><code>Fatal error: Call to a member function fetch_assoc() on a non-object.
  </code></pre>
  
  <p>Not sure what to do.  Any thoughts or help would be greatly appreciated!</p>