Solved

Transfer data between two databases

Posted on 2007-11-22
3
1,156 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Healthcheck 2 87
WordPress TK Title 8 25
unset shopping cart session 15 33
paypal ipn to mysql 3 13
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 …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now