您好我在使用sqlite数据库,我已经在运行的php中编写了一个查询。但是,我似乎无法构建一个解决方案,将在浏览器中显示此查询的结果(表格或其他)。这是我的代码。这是该系统的最后一个元素,因此感谢您的帮助。
代码:
<?php
$db = new PDO('sqlite:daypilot.sqlite');
$start = '2018-02-20';
$end = '2018-02-25';
$sql = 'SELECT * FROM events WHERE end > ? AND start < ?';
$stmt = $db->prepare($sql); $stmt->execute([$start, $end]);
$events = $stmt->fetchAll();
?>
[更新]评论,OP已尝试
<table>
<tr>
<th>id</th> <th>name</th> <th>contact</th>
</tr>
<?php foreach ($events as $event): ?>
<tr>
<td><?php echo $event['id'] ?></td>
<td><?php echo $event['name'] ?></td>
<td><?php echo $event['contact'] ?></td>
</tr>
<?php endforeach; ?>
</table>
答案 0 :(得分:3)
<?php
$db = new PDO('sqlite:daypilot.sqlite');
$start = '2018-02-20';
$end = '2018-02-25';
$sql = 'SELECT * FROM events WHERE end > ? AND start < ?';
$stmt = $db->prepare($sql); $stmt->execute([$start, $end]);
$events = $stmt->fetchAll(PDO::FETCH_ASSOC);
$table = '<table>';
foreach($events as $event) {
$table .= ' <tr>';
$table .= ' <td>' . $event['column_name'] . '</td>';
// repeat for every column you want to add
$table .= ' </tr>';
}
$table .= '</table>';
echo $table;
?>
嗯,这是构建HTML表的最简单(而不是最好)的方法。您应该将该代码段插入您希望表格显示的位置。
我将PDO::FETCH_ASSOC
传递给fetchAll方法以确保数据作为关联数组返回,然后你必须迭代它并提取所需的列