Jump to content

Why doesn't this script function?


RON_ron

Recommended Posts

:shrug: Why doesn't this code update two mysql databases (on two servers)? What am I doing wrong?

 

//SERVER 1 
$link = mysql_connect("localhost","usern1","pw1"); 
mysql_select_db("db_one1"); 

//SERVER 2 
$link = mysql_connect("xxx.xxx.xx.xxx","usern2","pw2"); 
mysql_select_db("db_one2"); 

$query = "INSERT INTO db1(subject, search, News, img) VALUES('$hsubject','$key','$news','$img')"; 
$result = mysql_query($query); 

$query = "INSERT INTO db2(subject, search, News, img) VALUES('$hsubject','$key','$news','$img')"; 
$result = mysql_query($query); 

$sentOk = "The data has been added to the database."; 
echo "sentOk=" . $sentOk; 

Link to comment
Share on other sites

Your code should look more like this: (didn't proof read it)

 


//SERVER 1 
$link = mysql_connect("localhost","usern1","pw1"); 
mysql_select_db("db_one1"); 

$query = "INSERT INTO db1(subject, search, News, img) VALUES('$hsubject','$key','$news','$img')"; 
$result = mysql_query($query); 

//SERVER 2 
$link = mysql_connect("xxx.xxx.xx.xxx","usern2","pw2"); 
mysql_select_db("db_one2"); 

$query = "INSERT INTO db2(subject, search, News, img) VALUES('$hsubject','$key','$news','$img')"; 
$result = mysql_query($query); 

$sentOk = "The data has been added to the database."; 
echo "sentOk=" . $sentOk; 


 

Because you rewrite the $link variable on the second connection...

This way you connect to the first db then send the info... then connect to second db and send info.

Link to comment
Share on other sites

check correct syntax

http://php.net/manual/en/function.mysql-query.php and http://www.php.net/manual/en/function.mysql-select-db.php

 

 

//SERVER 1 
$link = mysql_connect("localhost","usern1","pw1"); 
mysql_select_db("db_one1", $link); 

$query = "INSERT INTO db1(subject, search, News, img) VALUES('$hsubject','$key','$news','$img')"; 
$result = mysql_query($query, $link);

if(mysql_num_rows($result) == 0) {
$sentOk = "The data has been added to the database."; 
echo "sentOk=" . $sentOk;
}


//SERVER 2 
$link = mysql_connect("xxx.xxx.xx.xxx","usern2","pw2"); 
mysql_select_db("db_one2", $link); 

$query = "INSERT INTO db2(subject, search, News, img) VALUES('$hsubject','$key','$news','$img')"; 
$result = mysql_query($query, $link); 

if(mysql_num_rows($result) == 0) {
$sentOk = "The data has been added to the database."; 
echo "sentOk=" . $sentOk;
}

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.