简体   繁体   中英

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.

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 = "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:

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:

WHERE (isOfficial IS NULL or isOfficial IS NOT NULL)

which will becomes

WHERE 1 

or no need WHERE clause

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 } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM