Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li>First approach I would try: <ul> <li>run the query</li> <li>check if there were results returned</li> <li>if yes, run Your <code>initialize()</code> and <code>displayHtmlTable</code></li> <li>if <strong>no</strong>, run Your <code>initializeEmptyResult()</code> that will run the next query and just fills <code>$this-&gt;columnNames[]</code>:</li> </ul></li> </ol> <pre class="lang-sql prettyprint-override"><code>SELECT column_name FROM information_schema.columns WHERE table_name = 'my_table_name' </code></pre> <p>The query is dependent on MySQL bu there should be more alternatives for other databases, or still You can run <code>DESCRIBE table</code> and parse columns names from the result (should be SQL DBMS undependent).</p> <ol> <li><p>Second that is I think easier to accomplish: if there are no results returned from the query, do not display a coulmn names nor table at all, just display a message: 'No results found.'. This could be achieved within Your <code>displayHtmlTable</code> method:</p> <pre><code> public function displayHtmlTable() { $r = ''; $r .= '&lt;div class="sqlCommand"&gt;' . $this -&gt; sql . '&lt;/div&gt;'; if(count($this-&gt;records) &gt; 0) { $r .= '&lt;table&gt;'; $r .= '&lt;thead&gt;'; $r .= '&lt;tr&gt;'; foreach ($this-&gt;columnNames as $columnName) { $r .= '&lt;td&gt;' . $columnName . '&lt;/td&gt;'; } $r .= '&lt;/tr&gt;'; $r .= '&lt;/thead&gt;'; $r .= '&lt;tbody&gt;'; foreach ($this-&gt;records as $record) { $r .= '&lt;tr&gt;'; foreach ($record as $data) { $r .= '&lt;td&gt;' . $data . '&lt;/td&gt;'; } $r .= '&lt;/tr&gt;'; } $r .= '&lt;/tbody&gt;'; $r .= '&lt;table&gt;'; } else { $r .= '&lt;div class="no-results"&gt;No results found for query.&lt;/div&gt;'; } return $r; } </code></pre></li> </ol> <p>And You do not have to bother with column names...</p> <p><strong>EDIT:</strong> For the first case found a query for SQLite that should do the same as <code>select * from information_schema.columns where table_name = 'xxx'</code>:</p> <pre><code>PRAGMA table_info(table-name); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload