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.
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
Hope that helps!
Update