Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I get column names via PDO/Sqlite when query returns no records?
    primarykey
    data
    text
    <p>The following code allows me to pass an SQL statement to a class and call its method to display a nice table of the results, including column names.</p> <p>However, <strong>if there are no results, I still want the column names</strong> to be displayed.</p> <p>Unfortunately, <code>getColumnMeta</code> is not returning any data as it does in other examples I have found.</p> <p><strong>Does anyone know how to get getColumnMeta() working in this example, or another way I can get the names of the fields from the SQL statement when the query returns zero rows?</strong></p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt; &lt;style type="text/css"&gt; table thead tr td { background-color: #ddd; padding: 5px; font-weight: bold; } table tbody tr td { background-color: #eee; padding: 5px; color: navy; } div.sqlCommand { font-family: courier; } h2 { border-bottom: 1px solid #777; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;?php $sql = 'SELECT LastName,FirstName,Title FROM employee WHERE 1=2 ORDER BY LastName'; echo '&lt;h2&gt;sqlite&lt;/h2&gt;'; $dbSqlite = new DbSqlite($sql); echo $dbSqlite -&gt; displayHtmlTable(); class DbSqlite { protected $sql; protected $records = array(); protected $columnNames = array(); public function __construct($sql) { $this -&gt; sql = $sql; $this -&gt; initialize(); } protected function initialize() { $db = new PDO('sqlite:chinook.sqlite'); $result = $db -&gt; query($this -&gt; sql); $result -&gt; setFetchMode(PDO::FETCH_ASSOC); $columnsAreDefined = false; while ($row = $result -&gt; fetch()) { $this -&gt; records[] = $row; if (!$columnsAreDefined) { foreach ($row as $columnName =&gt; $dummy) { $this -&gt; columnNames[] = $columnName; } $columnsAreDefined = true; } } if (count($this -&gt; records) == 0) { $total_column = $result -&gt; columnCount(); var_dump($total_column); for ($x = 0; $x &lt; $total_column; $x++) { $meta = $result -&gt; getColumnMeta($x); //var_dump($meta); //bool(false) //$column[] = $meta['name']; } } } public function displayHtmlTable() { $r = ''; $r .= '&lt;div class="sqlCommand"&gt;' . $this -&gt; sql . '&lt;/div&gt;'; $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;'; return $r; } } ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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