Earlier I made a post on how to copy records between dissimilar tables, where the source table shares common fields with the target table.
This script will pull all data from one table (source table) where the field names are the same as a target table. All other fields in the target table will be left blank.
<?php
$db_cnx = mysql_connect('localhost', 'compasspoint', '*****');
//your input here
$sourceTable = "finan_clients";
$targetTable = "cpm_clients";
//you could also control the limit, the where, etc.
//get the field list from the target table
$fields = mysql_list_fields("compasspoint", "cpm_clients", $db_cnx);
$columns = mysql_num_fields($fields);
for ($i = 0; $i < $columns; $i++) {
$targetFieldList[count($targetFieldList)+1] = mysql_field_name($fields, $i);echo "<br>";
}
//get the field list from the source table
$fields = mysql_list_fields("compasspoint", "finan_clients", $db_cnx);
$columns = mysql_num_fields($fields);
for ($i = 0; $i < $columns; $i++) {
$sourceFieldList[count($sourceFieldList)+1] = mysql_field_name($fields, $i);echo "<br>";
}
$selectString = "insert into $targetTable
SELECT
";
//loop through the field list
foreach($targetFieldList as $n=>$v){
if(in_array($v, $sourceFieldList)){
$selectString .= "$v, ";
}else{
$selectString .= "CONCAT('') AS $v, ";
}
}
$selectString = substr($selectString, 0, strlen($selectString)-2);
$selectString .= " FROM $sourceTable";
echo "<br><pre>";
echo $selectString;
echo "</pre>";
mysql_query($selectString) or die(mysql_error());
?>