php scope of Global variables in a loop
<p>This code connects to a first database, loops and picks up a field called 'id' and use the id as a connection[database name] to another database in a function. All seemed to work except that in the function, the value of id does not change so cannot connect to db, even though it changes in the local variable. I suspect i need to unsett and set. Any help? Thanks</p>

<pre><code>&lt;?

$dbhost = "***";
$dbname = "users";
$dbuser = "****";
$dbpass = "***";

function myRecordHandler($record)
{
    global $dbhost;
    global $dbuser;
    global $dbpass;
    global $id;
    global $conn2;

    $db = mysql_select_db($id,$conn2) ;
    $quantity = $record["QUANTITY"];
    $price = $record["PRICE"];
    $mytotal ="INSERT into `mytotal`(`quantity`,`price`) VALUES ($quantity,$price)";
    mysql_query($mytotal,$conn2);
}

$conn1 = mysql_connect($dbhost, $dbuser, $dbpass, TRUE) or die("MySQL Error: " .mysql_error());
$conn2 = mysql_connect($dbhost, $dbuser, $dbpass, TRUE) or die("MySQL Error: " . mysql_error());

mysql_select_db($dbname,$conn1) or die("MySQL Error: " . mysql_error());
$query = "SELECT id,url FROM table userdata";
$result = mysql_query($query,$conn1);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    $id =$row['id'];
    $url=$row['url'];
    MagicParser_parse($url,"myRecordHandler","xml|PRODUCTS/PRODUCT/");
}
?&gt;
</code></pre>
