RON_ron Posted August 12, 2010 Share Posted August 12, 2010 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 More sharing options...
trq Posted August 12, 2010 Share Posted August 12, 2010 Your saving both connections within the one variable called $link. Hence, the second connection overrides the first. You then fail to pass any specific connection to mysql_query. Link to comment https://forums.phpfreaks.com/topic/210546-why-doesnt-this-script-function/#findComment-1098465 Share on other sites More sharing options...
Rohlan Posted August 12, 2010 Share Posted August 12, 2010 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 https://forums.phpfreaks.com/topic/210546-why-doesnt-this-script-function/#findComment-1098466 Share on other sites More sharing options...
PradeepKr Posted August 12, 2010 Share Posted August 12, 2010 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 https://forums.phpfreaks.com/topic/210546-why-doesnt-this-script-function/#findComment-1098467 Share on other sites More sharing options...
RON_ron Posted August 12, 2010 Author Share Posted August 12, 2010 Thanks mates! Before trying these, Just one more thing..... Do I have to give permission to update an MYSQL? E.g. If I'm on server 1 and using this script to update the mysql on server 1 and server 2. Link to comment https://forums.phpfreaks.com/topic/210546-why-doesnt-this-script-function/#findComment-1098472 Share on other sites More sharing options...
trq Posted August 12, 2010 Share Posted August 12, 2010 Do I have to give permission to update an MYSQL? Of course, as always, you need permissions to execute the queries. If its a remote server, you'll need permissions just to connect to it. Link to comment https://forums.phpfreaks.com/topic/210546-why-doesnt-this-script-function/#findComment-1098476 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.