2016-08-04 70 views
1

我是PHP和SQLite的新手。PHP中结构SQLite3查询

是否有可能在PHP中将SQLite3查询结构化为表格?

我有以下代码:

$result = $db->query('SELECT unique_id, description FROM dis_enums WHERE unique_id <= 12'); 
while ($row = $result->fetchArray()){ 
    print_r($row); 
    echo nl2br("\n"); 
} 

哪个返回如下:

Array([0] => 1 [unique_id] => 1 [1] => Concrete [description] => Concrete) 

Array([0] => 2 [unique_id] => 2 [1] => Bridge [description] => Bridge) 

有什么办法改变我的代码,以便它有头(UNIQUE_ID和描述)与下面的结果?

谢谢。

+0

使用'DISTINCT'为'SELECT DISTINCT UNIQUE_ID,说明...' – Saty

+0

@Saty谢谢你的回复。我以为DISTINCT是用来消除所有重复的记录并只提取唯一的记录?这不是我想要做的。如果我的问题不清楚,请道歉。我试图将我的查询结果安排成更具可读性的方式(带有标题的表格)。 – Breo

回答

0

您可以在表格的内容之前回显标题。
如果你想建立一个HTML表,将看起来像:

$result = $db->query('SELECT unique_id, description FROM dis_enums WHERE unique_id <= 12'); 
echo "<table>"; 
echo "<tr>"; 
echo "<th>unique_id</th><th>description</th>"; 
echo "</tr>"; 
while ($row = $result->fetchArray()){ 
    echo '<tr>'; 
    echo '<td>' . $row['unique_id'] . '</td>'; 
    echo '<td>' . $row['description'] . '</td>'; 
    echo '</tr>'; 
} 
echo "</table>"; 

这里有一个几乎与例如http://zetcode.com/db/sqlitephp/

+0

非常感谢! – Breo