Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7520431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:04:10+00:00 2026-05-30T02:04:10+00:00

I am not sure if I formatted the title well but this is continued

  • 0

I am not sure if I formatted the title well but this is continued from this question
How to do Mysql Union with check if row exists?

we got the UNION working well and combined 7 tables, issue now is that we want to give user the option to disable search from particular table. Sure you guessed it , sql returning error since if user wants data from last table we placed in UNION our sql starts with UNION instead SELECT ,

so we created multiple select list that returns array of available options
print_r($tables) returns Array ( [0] => table_1 [1] => table_2…

foreach ($tables as $key=> $source){
    if($key ==0){
        $UNION='';
    }else{
        $UNION='UNION ';
    }
}

this is perfect, only first one from array is not getting the UNION but in sql we have a problem since we cant loop same query and we cant get the $UNION var outside of the loop more than once(global var for example).

if (in_array("table_1", $tables)) {
    $sql1 ="".$UNION."SELECT  i.title,i.category,'a' as source FROM table_a  WHERE REGEXP 'news'";
}
if (in_array("table_2", $tables)) {
    $sql2 ="".$UNION."SELECT i.title,i.category, 'b' as source FROM table_b WHERE REGEXP 'news'";
}


$query=$sql1.$sql2;

we tried to get the var $UNION outside the foreach loop by placing it in array and imploding but that does not work either, for example

foreach ($tables as $key=> $source){
    if($key ==0){
        $UNION='';
    }else{
        $UNION='UNION ';
    }
 $get_union[]=$UNION;
}

if (in_array("table_1", $tables)) {
    $sql1 ="".implode($UNION)."SELECT  i.title,i.category,'a' as source FROM table_a  WHERE REGEXP 'news'";
}
if (in_array("table_2", $tables)) {
    $sql2 ="".implode($UNION)."SELECT i.title,i.category, 'b' as source FROM table_b WHERE REGEXP 'news'";
}

i was just asked to edit the question so here is result I would need

user selects option from multiple select list to disable table_b and table_c
our multi select option outputs and array. I need to make sure that first in array does ot have UNION as prefix and than run the query. So something like this:

$table =array([0]=>table_a,[1]=>table_b,[2]=>table_c);

if($table[0]){
   $UNION='';
}else{
    $UNION='UNION ';
}

if (in_array("table_a", $tables)) {
    $sql1 ="".$UNION."SELECT  i.title,i.category,'a' as source FROM table_a  WHERE REGEXP 'news'";
}
if (in_array("table_b", $tables)) {
    $sql2 ="".$UNION."SELECT i.title,i.category, 'b' as source FROM table_b WHERE REGEXP 'news'";
}
if (in_array("table_c", $tables)) {
    $sql3 ="".$UNION."SELECT i.title,i.category, 'c' as source FROM table_b WHERE REGEXP 'news'";
}

$query=$sql1.$sql2.$sql2;

Any brave soul that can help out please do. Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:04:11+00:00Added an answer on May 30, 2026 at 2:04 am

    So you want to achieve a similar result to this:

    SELECT  i.title,i.category,'a' as source FROM table_a  WHERE REGEXP 'news'
    UNION 
    SELECT i.title,i.category, 'b' as source FROM table_b WHERE REGEXP 'news'
    

    given $tables = array('table_a', 'table_b',...);

    The problem is that you don’t have a way to match ‘table_a’ to ‘a’ (unless of course these were your real table names, which I seriously doubt that)

    So, what you need to do is to create a hastable to match each table to the fixed values you want to fix to them:

    $tablesAndValues = array (
        'table_a'   => 'a',
        'users'     => 'address',
        ...
    );
    

    Once you have that the queries can be created this way:

    $sqlArray = array();
    for ($tables as $table => $value) {
        $sqlArray[] = "SELECT i.title, i.category, '" . $value . "' as source FROM " . $table . " WHERE REGEXP 'news'";
    }
    

    Now let’s just implode the array:

    $sql = implode(" UNION ", $sqlArray);
    

    I don’t know much of PHP but this should give you an idea of how to solve it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is likely to be a(nother) noob question, but I'm not sure how to
I'm not sure if this is a simple question or not, but I'd like
Not sure how to ask this, but i'll give it a try: I have
Not sure if this is a big deal. But wondering why when the site
Not sure if this is the correct Stack Exchange website but here goes.. A
I'm sure this is possible some how, but what I'm doing is not working.
Edited this question entirely, not sure if it made sense previously. Ok, I am
Not sure if I've worded my title correctly but here goes. I have a
Hopefully this is a simple one, but not sure if it will be as
Not sure whats going on here, or what could be the integer in this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.