[英]SQL Query: Hide items based on column value?
I am pulling some data from table in a MySQL database. 我从MySQL数据库中的表中提取一些数据。 Inside this table is a column called "hidden", by default it is set to 0, but when an item needs to be hidden, it will be set to 1. So I need a code that I can use that will hide all rows if the "hidden" column is set to 1. 在这个表中是一个名为“hidden”的列,默认情况下它被设置为0,但是当一个项需要被隐藏时,它将被设置为1.所以我需要一个我可以使用的代码,它将隐藏所有行“隐藏”列设置为1。
Lets say my code is something simple like this: 让我们说我的代码很简单:
<?php
$result = mysql_query("SELECT * FROM clients ORDER BY name")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo $row['name'];
}
?>
Is there a simple code I can add to it? 我可以添加一个简单的代码吗?
Add WHERE
clause on it 在其上添加WHERE
子句
SELECT *
FROM clients
WHERE hidden = 0
ORDER BY name
The query above will only show items that are not hidden. 上面的查询仅显示未隐藏的项目。
$result = mysql_query("SELECT * FROM clients WHERE hidden = 0 ORDER BY name");
这可能是最简单的方法。
There are 2 options: 有两种选择:
1) if you always want those rows hidden then you don't even have to load them by altering your query. 1)如果您总是希望隐藏这些行,那么您甚至不必通过更改查询来加载它们。
SELECT * FROM clients WHERE hidden=0 ORDER BY name;
2) You can check that value in the while loop. 2)您可以在while循环中检查该值。
while($row = mysql_fetch_array( $result )) {
if($row['hidden']==0) echo $row['name'];
}
Your current query 您当前的查询
SELECT * FROM clients ORDER BY name
retrieves every row in the table. 检索表中的每一行。 If you want to hide (not retrieve) rows where the value of the hidden
column is 1
then just add a WHERE clause to check that hidden
is not equal to 1
. 如果要隐藏(不检索) hidden
列的值为1
行,则只需添加WHERE子句以检查hidden
是否不等于1
。
SELECT * FROM clients WHERE hidden = 0 ORDER BY name
<?php
$result = mysql_query("SELECT * FROM clients WHERE hidden=0 ORDER BY name")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo $row['name'];
}
?>
$result = mysql_query("SELECT * FROM clients WHERE hidden=0 ORDER BY name");
will return only the rows which are not hidden. 将仅返回未隐藏的行。
Also, if this is a new program you are going to write, use MySqli or PDO instead of the mysql_* functions since they are deprecated. 此外,如果这是您要编写的新程序,请使用MySqli或PDO而不是mysql_ *函数,因为它们已被弃用。 See - http://us.php.net/mysqli and http://us.php.net/pdo 请参阅 - http://us.php.net/mysqli和http://us.php.net/pdo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.