I have a table of users which i get ids and urls. I use these ids as connection to another databases in a recursive loop. In any of these database connections, i call the function myRecordHandler and supply to it the url.
Now, i get this error, Cannot redeclare myrecordhandler() . How can I effectively escape this? I have read articles relating to this, but seems this is quite different problem. thanks.
require("MagicParserISO88591.php");
$dbhost = "***";
$dbname = "users";
$dbuser = "****";
$dbpass = "***";
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
$query = "SELECT id,url FROM table userdata";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$id =$row['id'];
$url=$row['url'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
$db = mysql_select_db($id) ;
function myRecordHandler($record)
{
$quantity = $record["QUANTITY"];
$price = $record["PRICE"];
$mytotal ="INSERT into `mytotal`(`quantity`,`price`)
VALUES ($quantity,$price)
}
MagicParser_parse($url,"myRecordHandler","xml|PRODUCTS/PRODUCT/");
}
Thanks that worked!! Anyway, as said, function myRecordHandler did not work because i had no connections in the functions. I need the current connection[database] in the while loop to be used in the function too. I tried global variables but will not work. Any help on that?
Hi,
I've reworked your original code into the example below - this should get you close; notice how the optional 4th parameter to mysql_connect() is used within myRecordHandler and set to TRUE to open a new link, and all necessary variables declared as global;
require("MagicParserISO88591.php");
$dbhost = "***";
$dbname = "users";
$dbuser = "****";
$dbpass = "***";
function myRecordHandler($record)
{
global $dbhost;
global $dbuser;
global $dbpass;
global $id;
$conn2 = mysql_connect($dbhost, $dbuser, $dbpass, TRUE) or die("MySQL Error: " . mysql_error());
$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);
}
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
$query = "SELECT id,url FROM table userdata";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$id =$row['id'];
$url=$row['url'];
MagicParser_parse($url,"myRecordHandler","xml|PRODUCTS/PRODUCT/");
}
Hope this helps!
Cheers,
David.
--
MagicParser.com
Thanks Dave.should work, but it seems to lose the connection inside the function myRecordHandler. rocking my head!!
Hi,
Ah - I didn't include mysql_close() code in the above, so you may be reaching the connection limit. That could be added, but the best approach in terms of performance would be to create the 2 connections globally rather than connect / disconnect for every record - for example:
require("MagicParserISO88591.php");
$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/");
}
Hope this helps!
Cheers,
David
--
MagicParser,com
Thanks Dave. There is some slight problem here. The value of $id does not change within the function, therefore no database connection inside the function.
I suspect the problem is that myRecordHandler() is being called from within MagicParser_parse(). It's probably being called within a separate execution context that doesn't share the same global name space.
Any help to get $id have the value from
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$id =$row['id'];
$url=$row['url'];
MagicParser_parse($url,"myRecordHandler","xml|PRODUCTS/PRODUCT/");
}
Hi,
Ah - if the code above is not within the global space (for example if being executed within a content management system by a PHP exec plugin) then $id would have to be declared as global within the main code - for example:
global $id;
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$id =$row['id'];
$url=$row['url'];
MagicParser_parse($url,"myRecordHandler","xml|PRODUCTS/PRODUCT/");
}
Hope this helps!
Cheers,
David.
Hi bayuobie,
Your myrecordhandler() function needs to be declared outside of the while loop - that should be all is, for example:
function myRecordHandler($record)
{
$quantity = $record["QUANTITY"];
$price = $record["PRICE"];
$mytotal ="INSERT into `mytotal`(`quantity`,`price`) VALUES ($quantity,$price)";
}
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$id =$row['id'];
$url=$row['url'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
$db = mysql_select_db($id) ;
MagicParser_parse($url,"myRecordHandler","xml|PRODUCTS/PRODUCT/");
}
Note that there is not yet any mysql_query code within the record handler function after constructing the SQL in $mytotal - bear in mind when you come to add this that since you are using a while() loop to traverse the results of a previous query this will have to made using a separate database link created especially for that process)...
Hope this helps!
Cheers,
David
--
MagicParser.com