?
Solved

Transfer data between two databases

Posted on 2007-11-22
3
Medium Priority
?
1,161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 17

Assisted Solution

by:Aleksandar Bradarić
Aleksandar Bradarić earned 800 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 1200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this article, we’ll look at how to deploy ProxySQL.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

719 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