MyBB Community Forums

Full Version: read from mybb database and write to external database ?!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I want to define a task that reads some elements from mybb db and writes them in an external db, i have written a php code like this for doing the task :

<?php
$query = $db->simple_select("users", "uid,email,timeonline");
while ($info = $db->fetch_array($query))
	$this->connection = mysql_connect("External DB IP", "User", "Pass");
	mysql_select_db("DB Name", $this->connection);
	$email = $info['email'];
	$q = "SELECT timeonline FROM table_name WHERE email = '$email'";
	$result = mysql_query($q, $this->connection);
	if(!$result || (mysql_numrows($result) < 1)){
		}
	else {
		$value = $info['timeonline'];
		$q = "UPDATE table_name SET timeonline = '$value' WHERE email = '$email'";
		mysql_query($q, $this->connection);
	}
	mysql_close($this->connection);
	$value = 0;
	$db->update_query("users", array('timeonline' => '$value'), 'uid=$info[\'uid\']';
?>

I will give a little explanation about code, it first gets all users of mybb by uid,email,timeonline then connects to external db searches for each email, if email is available in external DB it will set timeonline value of that user with value reads from mybb DB then closes external DB and sets those users timeonline to 0 in mybb DB.

Now the above just simply doesn't work Big Grin can anyone help me to find out what i'm doing wrong ?!
mysql connect in a while loop?
(2012-11-25, 07:52 PM)frostschutz Wrote: [ -> ]mysql connect in a while loop?

Any better way to work with two database ?
A friend told me to save values I want to change in arrays and update database in the end but anyway thar didn't work either
My main question is that is this way of working with two databases (one from mybb one external) correct ?
Connect before the while loop. In fact connect before anything else as you may not be able to connect in the first place, in which case you don't have to do anything else.

Use the connection you made in the loop if you must (lots of queries).

Working examples: http://www.php.net/manual/en/function.mysql-connect.php

but mysql_*() is deprecated, use mysqli_ http://www.php.net/manual/en/mysqli.construct.php or PDO
do you want your task to be run from within MyBB using its task system, or is this an external only deal?