简体   繁体   中英

php/mysql SHOW COLUMNS from table1 and table2

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 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.

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";

将查询更改为:

$result = mysql_query("SELECT column_name FROM information_schema WHERE table_name IN ('".$table."','".$table2."' WHERE column_name NOT IN ('ID','Key','Text');");

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