Solved

Transfer data between two databases

Posted on 2007-11-22
3
1,158 Views
Last Modified: 2008-02-01
Hi,

Is there a way to select data from one database and insert them into another but using the same php page? I want to copy a table from one database to another but doing it with export or backup will not work (I have a strange problem with encoding).

Thanks!
0
Comment
Question by:infodigger
3 Comments
 
LVL 17

Assisted Solution

by:leannonn
leannonn earned 200 total points
ID: 20333665
Try this:
CREATE TABLE db2.your_table SELECT * FROM db1.your_table_too

Open in new window

0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 300 total points
ID: 20333850
I dont think the above query can  run from php.

If it doesnt work try the code below
<?
### CONNECT TO 1ST DATABASE ########
 
$db1 = mysql_connect("localhost","username","pass");
 
mysql_select_db("dbname",$db1);
 
$table = "table_name"; // put name of your table here
 
 
$q = mysql_query("SHOW CREATE TABLE ".$table);
 
 
$create = mysql_fetch_row($q);
 
 
	$value = '';
	
	$result = mysql_query('SELECT * FROM `'.$table.'` ');
	
	$i = 0;
	
	while ($row = mysql_fetch_row($result)){
		
		$values = '';
		
		foreach ($row as $data){
		
			$values .= '\'' . addslashes($data) . '\', ';
		
		}
		
		$values = substr($values, 0, -2);
		
		if(!$i) {
		
			$value .= ' VALUES (' . $values . ') ' ;
		
		}else{
		
			$value .= ' ,(' . $values . ') ' ;
		
		}
		$i++;
		
	}
		
	$value = "INSERT INTO `".$table."` ".$value.";";
	
 
 
### CONNECT TO 2ST DATABASE ########
 
 
$db2 = mysql_connect("localhost","username","pass");
 
mysql_select_db("dbname",$db2);
 
	
mysql_query($create[1]) or die(mysql_error());
 
mysql_query($value) or die(mysql_error());

Open in new window

0
 

Author Comment

by:infodigger
ID: 20333884
Thanks!
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question