我有一段时间搞清楚这一点。
我能够从数据库中提取数据没问题并显示所有结果,但是,我想限制返回的结果数量,我还想只显示状态为1的结果忽略所有0。
这是我到目前为止所拥有的......
<?php
$query = "SELECT * FROM " . $dbtable . " ORDER by `id` DESC;";
$result = mysql_query($query) or die('Error: ' . mysql_error());
?>
...other stuff...
<?php
while ( $row = mysql_fetch_array( $result ) ) {
$username = $row["username"];
$user_photo = $row["user_photo"];
$location = $row["location"];
$status = $row["status"];
?>
<li><img src="<?= $user_photo ?>" class="collab" data-username="<?= $username ?>" data-location="<?= $location ?>" /></li>
<?php } ?>
&#13;
基本上,我想要返回状态为1的前30个结果。这可能吗?我不知道如何处理这个问题。
非常感谢任何帮助。
答案 0 :(得分:4)
使用WHERE
条款。
... WHERE status = 1 ORDER BY `id` DESC LIMIT 30
有关更多信息,请访问MySQL.com上的以下内容:
从手册:
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], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[INTO OUTFILE 'file_name' export_options
| INTO DUMPFILE 'file_name'
| INTO var_name [, var_name]]
[FOR UPDATE | LOCK IN SHARE MODE]]
答案 1 :(得分:2)
可以通过查询完成过滤行和限制结果。 where
子句将允许您过滤行,而limit
子句将允许您限制返回的结果,不出所料:
SELECT *
FROM $dbtable
WHERE status = 1
ORDER BY id DESC
LIMIT 30