Solved

Transfer data between two databases

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
The viewer will learn how to count occurrences of each item in an array.
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 …

623 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