fer0an Posted October 17, 2010 Share Posted October 17, 2010 hello I want query from one table and insert in another table on another domain . each database on one domain name. for example http://www.site.com $con1 and http://www.site1.com $con. can anyone help me? my code is : <?php $dbuser1 = "insert in this database"; $dbpass1 = "insert in this database"; $dbhost1 = "localhost"; $dbname1 = "insert in this database"; // Connecting, selecting database $con1 = mysql_connect($dbhost1, $dbuser1, $dbpass1) or die('Could not connect: ' . mysql_error()); mysql_select_db($dbname1) or die('Could not select database'); $dbuser = "query from this database"; $dbpass = "query from this database"; $dbhost = "localhost"; $dbname = "query from this database"; // Connecting, selecting database $con = mysql_connect($dbhost, $dbuser, $dbpass) or die('Could not connect: ' . mysql_error()); mysql_select_db($dbname) or die('Could not select database'); //query from database $query = mysql_query("SELECT * FROM `second_content` WHERE CHANGED =0 limit 0,1"); while($row=mysql_fetch_array($query)){ $result=$row[0]; $text=$row[1]."</br>Size:(".$row[4].")"; $alias=$row[2]; $link = '<a target="_blank" href='.$row[3].'>Download</a>'; echo $result; } //insert into database mysql_query("SET NAMES 'utf8'", $con1); $query3= " INSERT INTO `jos_content` (`id`, `title`, `alias`, `) VALUES (NULL, '".$result."', '".$alias."', '')"; if (!mysql_query($query3,$con1)) { die('Error: text add' . mysql_error()); } mysql_close($con); mysql_close($con1); ?> Quote Link to comment https://forums.phpfreaks.com/topic/216070-query-from-a-one-database-and-insert-in-another-database/ Share on other sites More sharing options...
ignace Posted October 17, 2010 Share Posted October 17, 2010 This script is incomplete and needs tweaking (hostname, user, pass, error descriptions, ..). It uses the SELECT .. INTO OUTFILE and LOAD DATA INFILE SQL commands. try { // database server #1 $db1 = db('db', 'pass', 'user', 'server1'); $outfile = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'outfile.txt'; if(!mysql_query("SELECT * FROM table INTO OUTFILE '$outfile'")) throw new Exception(''); if(!file_exists($outfile) || filesize($outfile) == 0) throw new Exception(''); // database server #2 $db2 = db('db', 'pass', 'user', 'server2'); if(!mysql_query("LOAD DATA INFILE '$outfile' REPLACE INTO TABLE table")) throw new Exception(''); } catch(Exception $e) { echo $e->getMessage(); } function db($database, $pass, $user = 'root', $host = 'localhost') { $db = mysql_connect($host, $user, $pass); if($db == FALSE) throw new Exception(''); if(!mysql_select_db($database, $db)) throw new Exception(''); return $db; } Quote Link to comment https://forums.phpfreaks.com/topic/216070-query-from-a-one-database-and-insert-in-another-database/#findComment-1122937 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.