[英]Select from same SQL table twice using PHP
I'm creating a site in PHP that lists the userbars of a forum, and have a column named 'isOfficial' - I've been able to select both NULL and IS NOT NULL seperately but am not sure how to select both. I'm creating a site in PHP that lists the userbars of a forum, and have a column named 'isOfficial' - I've been able to select both NULL and IS NOT NULL seperately but am not sure how to select both.
As best as I can describe it, I'd like to be able to run these both from the same table:尽我所能描述它,我希望能够从同一个表中运行它们:
First Query:第一个查询:
<?php include_once("assets/php/db.php");
$sql = "SELECT ubFilename, ubGroupName, ubGroupOwner, ubOwnerLink, isOfficial, isActiveGroup FROM UBSUserbars WHERE NOT (isOfficial <=> NULL) ORDER BY ubGroupName";
$resultset = mysqli_query($conn, $sql);
while( $record = mysqli_fetch_assoc($resultset) ) {
?>
Second Query:第二个查询:
<?php include_once("assets/php/db.php");
$sql = "SELECT ubFilename, ubGroupName, ubGroupOwner, ubOwnerLink, isOfficial, isActiveGroup FROM UBSUserbars WHERE (isOfficial <=> NULL) ORDER BY ubGroupName";
$resultset = mysqli_query($conn, $sql);
while( $record = mysqli_fetch_assoc($resultset) ) {
?>
And have them both output as such on the same page:并将它们都放在同一页面上:
<div class="cards">
<?php
include_once("assets/php/db.php");
$sql = "SELECT ubFilename, ubGroupName, ubGroupOwner, ubOwnerLink, isOfficial, isActiveGroup FROM UBSUserbars WHERE NOT (isOfficial <=> NULL) ORDER BY ubGroupName";
$resultset = mysqli_query($conn, $sql);
while( $record = mysqli_fetch_assoc($resultset) ) {
?>
<div class="col-sm-4 col-card <?php echo $record['isActiveGroup'] === 'active' ? 'active-group' : ''; ?>">
<img src="i/OGUsers/<?php echo $record['ubFilename']; ?>">
<hr>
<h4 class="group"><?php echo $record['ubGroupName']; ?></h4>
<span>Owner: <a class="owner" href="<?php echo $record['ubOwnerLink']; ?>"><?php echo $record['ubGroupOwner']; ?></a></span>
</div>
<?php } ?>
How about remove your WHERE clause in your SQL query:如何删除 SQL 查询中的 WHERE 子句:
$sql = "SELECT ubFilename, ubGroupName, ubGroupOwner, ubOwnerLink, isOfficial, isActiveGroup FROM UBSUserbars ORDER BY ubGroupName";
If you want to select both A and B value in a column your WHERE clause should be:如果你想 select 列中的 A 和 B 值,你的 WHERE 子句应该是:
WHERE (table.column = A or table.column = B)
In your case, you want to select both NULL and NOT NULL value in isOfficial
column so WHERE clause will be:在您的情况下,您希望 select NULL 和 NOT NULL 值在isOfficial
列中,因此 WHERE 子句将是:
WHERE (isOfficial IS NULL or isOfficial IS NOT NULL)
which will becomes这将成为
WHERE 1
or no need WHERE clause或者不需要 WHERE 子句
Your code will becomes:您的代码将变为:
<div class="cards">
<?php
include_once("assets/php/db.php");
$sql = "SELECT ubFilename, ubGroupName, ubGroupOwner, ubOwnerLink, isOfficial, isActiveGroup FROM UBSUserbars ORDER BY ubGroupName";
$resultset = mysqli_query($conn, $sql);
while( $record = mysqli_fetch_assoc($resultset) ) {
?>
<div class="col-sm-4 col-card <?php echo $record['isActiveGroup'] === 'active' ? 'active-group' : ''; ?>">
<img src="i/OGUsers/<?php echo $record['ubFilename']; ?>">
<hr>
<h4 class="group"><?php echo $record['ubGroupName']; ?></h4>
<span>Owner: <a class="owner" href="<?php echo $record['ubOwnerLink']; ?>"><?php echo $record['ubGroupOwner']; ?></a></span>
</div>
<?php } ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.