Jump to content

query from a one database and insert in another database


fer0an

Recommended Posts

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);
             


?>

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.