Hope this helps someone ... Here is a little PHP script I wrote if you need to copy some columns but not others, and / or the columns are not in the same order in both tables. As long as the columns are called the same, this will work. Therefore, if table A has [userid, handle, something] and tableB has [userID, handle, timestamp], then you would "SELECT userID, handle, NOW () as the timestamp FROM tableA", then get the result of this and pass the result as the first parameter of this function ($ z). $ toTable is the string name for the table you are copying to, and $ link_identifier is the db you are copying to. It is relatively fast for small data sets. It is not recommended that you try to move more than several thousand lines at a time this way in production settings. I use this primarily to back up the data collected during the session when the user logs out and then immediately flushes the data from live db to keep it thin.
function mysql_multirow_copy($z,$toTable,$link_identifier) { $fields = ""; for ($i=0;$i<mysql_num_fields($z);$i++) { if ($i>0) { $fields .= ","; } $fields .= mysql_field_name($z,$i); } $q = "INSERT INTO $toTable ($fields) VALUES"; $c = 0; mysql_data_seek($z,0); //critical reset in case $z has been parsed beforehand. ! while ($a = mysql_fetch_assoc($z)) { foreach ($a as $key=>$as) { $a[$key] = addslashes($as); next ($a); } if ($c>0) { $q .= ","; } $q .= "('".implode(array_values($a),"','")."')"; $c++; } $q .= ";"; $z = mysql_query($q,$link_identifier); return ($q); }
Josh Strike Nov 10 '09 at 20:05 2009-11-10 20:05
source share