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
https://forums.phpfreaks.com/topic/210546-why-doesnt-this-script-function/
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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.