Skip to main content
3 of 3
added 121 characters in body
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Conditional row count across 4 tables

I have the following function that works fine. Is there a better way to do it and will this be efficient if I have 100+ rows returned from each table?

The database

Tables

  • wcx_articles
  • wcx_videos
  • wcx_apps
  • wcx_links

The corresponding _active column for each table

  • article_active
  • video_active
  • app_active
  • link_active

_active is a switch 1 or 0

This is my current function

public function findInactiveItems() {
        
    $query = "SELECT * FROM wcx_articles WHERE article_active = '0'
    UNION
    SELECT * FROM wcx_videos WHERE video_active = '0'
    UNION
    SELECT * FROM wcx_apps WHERE app_active = '0'
    UNION
    SELECT * FROM wcx_links WHERE link_active = '0'
    ";
        if( $this->num_rows( $query ) > 0 ) {    
            $count = $this->num_rows($query);
            return $count;
        }
    }

 public function num_rows( $query ) {
    $query = $this->link->query( $query );
    if( mysqli_error( $this->link ) ) {
        $this->log_db_errors( mysqli_error( $this->link ), $query, 'Fatal' );
        return mysqli_error( $this->link );
    }
    else {
        return mysqli_num_rows( $query );
    }
    mysqli_free_result( $query );
}
CodeX
  • 1.1k
  • 2
  • 13
  • 19