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

  • Home
  • SEARCH
  • 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 8363773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:16:02+00:00 2026-06-09T12:16:02+00:00

Can someone please advise me how to save fields from two database tables? This

  • 0

Can someone please advise me how to save fields from two database tables?

This works fine for one table:

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

$result = mysql_query("SHOW COLUMNS FROM ".$table." WHERE Field NOT IN
('ID','Key','Text');");

$i = 0;
if (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_assoc($result)) {
        $csv_output .= $row['Field']."; ";
        $i++;
    }
}
$csv_output .= "\n";`

I need fields from two different tables. I have two db’s on same machine. Field names are differnet on tables.

This is not working:

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
mysql_select_db($db2) or die("Can not connect.");

$result = mysql_query("SHOW COLUMNS FROM ".$table." , ".$table2."  WHERE Field NOT IN 
('ID','Key','Text');");

$i = 0;
if (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_assoc($result)) {
        $csv_output .= $row['Field']."; ";
        $i++;
    }
}
$csv_output .= "\n";

Fields “ID”, “Key” and “Text” exists only on DB1.

  • 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-06-09T12:16:03+00:00Added an answer on June 9, 2026 at 12:16 pm

    The problem you’re having lies in selecting the DB. After you’ve selected the first DB you immediately select the second, which overwrites the previous, so the selected DB is the second one at the end.
    One solution would be to select the first database, get the column names you need, save them in your $csv_output, then select the second DB, get the columns you need and append them to the $csv_output.
    The solution Usman proposed is also good, the way to do it is the following

    USE information_schema;  -- select the correct DB
    SELECT column_name FROM columns WHERE table_name IN ('table1', 'table2');
    

    Hope that helps!

    Update

    $link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
    
    // get the column name from the first DB
    mysql_select_db($db1, $link) or die("Can not connect to DB1.");
    $result = mysql_query("SHOW COLUMNS FROM ".$table." WHERE Field NOT IN
    ('ID','Key','Text')");    
    $i = 0;
    if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_assoc($result)) {
            $csv_output .= $row['Field']."; ";
            $i++;
        }
    }
    
    // get the column names from the second DB
    mysql_select_db($db2, $link) or die("Can not connect to DB2.");
    $result = mysql_query("SHOW COLUMNS FROM ".$table." WHERE 1");
    $i = 0;
    if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_assoc($result)) {
            $csv_output .= $row['Field']."; ";
            $i++;
        }
    }
    $csv_output .= "\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please explain to me how this responsive approach works? This was done
Can someone please advise a good approach to this problem: On page load, an
Can someone please advise how to do this search and replace in textmate. I
Can someone please advise on uploading files to Office365 SharePoint? I believe this should
Can someone please tell me what this means: 07-04 09:54:38.048: I/DetailActivity(15496): Title that is
Can someone please explain how Read/Show works.. I cannot find any tutorials on it.
Please advise whether I am hallucinating... I hope so because I can't get tables
Can someone please advise on uploading a file using the DAV protocol in PHP?
Can someone please advise on how to define & map (using annotations) custom types
Can someone please explain why ?___SID=U is appearing in some Magento URLs on my

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.