经过艰苦的努力之后,我终于能够在我的index.php
中显示博客条目了,但订购的是最新的博客条目,而较旧的条目位于最顶层。
这是我的代码:
<?php
$connect = new PDO('mysql:host=localhost;dbname=posts','root','');
$cmd = $connect->query('SELECT * FROM posts');
$resultado = $cmd->fetchAll(PDO::FETCH_ASSOC);
foreach($resultado as $item):
?>
<div class="post">
<div class="post-body">
<a href="" class="post-title"><?php echo $item['titulo']?></a>
<div class="title-separator"></div>
<p class="post-body-text"><?php echo $item['cuerpo'] ?></p>
</div>
<div class="post-body-footer">
<img src="images/posted.png" alt="" class="posted"> <span class="posted-text">Posted on </span><span class="date"><?php echo $item['fecha']?></span>
</div>
</div>
<?php
endforeach;
?>
答案 0 :(得分:2)
为了&#34;命令&#34;您希望将它们列为的时尚数据,您可以使用
ORDER BY your_column
可以选择ASC
或DESC
。
ORDER BY
默认为ASC
。
I.e。:
SELECT * FROM posts ORDER BY titulo DESC
请教:
从手册中摘录的例子:
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]