Solved

Transfer data between two databases

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Router for PHP reqeusts 12 32
Wordpress Query 1 34
Error installing extension Linux 4 34
Filter wordpress query 3 12
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Creating and Managing Databases with phpMyAdmin in cPanel.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 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

19 Experts available now in Live!

Get 1:1 Help Now