I have set up four websites for local charities but they want the same events to appear on all websites. The sites have all been set up using Joomla and are on the same server but different domains. A common connection to MySQL is not a problem.
What I am trying to do is collect data from the MySQL database of the main site and send it to the other sites using PHP (will use a cron job when I get the script working). There is only one table that needs to be transfered.
The script I have come up with so far is:
<?php
$db_host = "localhost";
$table1 = "jos_extcal_events"; $table2 = "jos_extcal_events";
$db_name1 = "midsuffo2"; $db_user1 = "user1"; $db_pw1 = "password1";
$db_name2 = "tbrn1"; $db_user2 = "user2"; $db_pw2 = "password2";
$resource1 = mysql_connect($db_host, $db_user1, $db_pw1)or die ('I cannot connect to the database because: ' . mysql_error());;
//select query to get all the data from resource 1 mysql_select_db ($db_name1)or die("Unable to select database $db_name1"); $sql = "SELECT * FROM $table1"; $query = mysql_query($sql, $resource1); for ($i=0;$i<($result = mysql_fetch_assoc($query));$i++)
//insert data into $resource2 $resource2 = mysql_connect($db_host, $db_user2, $db_pw2)or die ('I cannot connect to the database because: ' . mysql_error());; mysql_select_db ($db_name2)or die("Unable to select database $db_name2"); { mysql_query("INSERT INTO $table2"); }
mysql_close($resource1); mysql_close($resource2);
?>
Although it runs without error, it doesn't do anything!
I would prefer to drop the old table and create a new one otherwise deleted events would stay on the client sites.
Can anyone help or suggest an easier way of doing it?
Many thanks
Tony
"Tony" tony@ttiger.co.uk wrote: [...]
$resource1 = mysql_connect($db_host, $db_user1, $db_pw1)or die ('I cannot connect to the database because: ' . mysql_error());;
//select query to get all the data from resource 1 mysql_select_db ($db_name1)or die("Unable to select database $db_name1"); $sql = "SELECT * FROM $table1"; $query = mysql_query($sql, $resource1); for ($i=0;$i<($result = mysql_fetch_assoc($query));$i++)
This for appears to have no {}d body so the next single statement gets looped:
//insert data into $resource2 $resource2 = mysql_connect($db_host, $db_user2, $db_pw2)or die ('I cannot connect to the database because: ' . mysql_error());;
so that's connecting to the database count(*) times without disconnecting, which probably isn't what you wanted to do!
mysql_select_db ($db_name2)or die("Unable to select database $db_name2"); {
I think the for() above should be just before this open-brace.
Also, I think you should be clearer with all mysql_select_db and mysql_query commands which $resource they should work on. PHP says it will use the last mysql_connect()d resource, but that way madness lies.
mysql_query("INSERT INTO $table2"); }
mysql_close($resource1); mysql_close($resource2);
?>
You do know I want danger money for the mental anguish of PHP?
Hope that helps,