PDA

View Full Version : Fatal error while adding users using a script


nainil
07-19-2011, 09:45 PM
When I run the below query it tells me:

Array ( [0] => That username is already in use or does not meet the administrator's standards. If you are mcar and you have forgotten your password, click here. ) 0 Failure
hello no newuserid

Fatal error: Registry object is not an object in [path]/includes/class_dm.php on line 205

-------------------------------

If there are errors while registering a user I want the script to ignore them. I don't want the script to stop. I believe the Fatal Error is due to 2 instances of database calls. I am new here so am wondering if someone can help show the right way to get this script correct so it pulls up records from one table and uses them to create users in vbulletin.

<?php

function register_in_vb($username, $password, $email)
{
define('VB_AREA', 'External');
define('SKIP_SESSIONCREATE', 0);
define('SKIP_USERINFO', 1);
//define('CWD', './../../forum/' );
require_once('includes/init.php');
require_once('includes/functions_misc.php');

$registry = $vbulletin;
unset($vbulletin);
$vbDb = $registry->db;
//declare as global vbulletin's registry and db objects
global $vbulletin,$db;
$vbulletin = $registry;
//backup the original $db object (new!!)
$backupdb = $db;
$db = $vbDb;

$newuser =& datamanager_init('User', $vbulletin, ERRTYPE_ARRAY);
$newuser->set('username', $username);
$newuser->set('email', $email);
$newuser->set('password', $password);
$newuser->set('usergroupid', 2);

$newuser->pre_save();

if(empty($newuser->errors)){ // success
$db = $backupdb;
echo 1;
echo " Success <br>";
return $newuser->save();

}else{ // failure
$db = $backupdb;
print_r( $newuser->errors);
echo 0;
echo " Failure <br>";
return false;
}

}


// For Local Database
$dbhostname = "localhost";
$dbusername = "dbuser";
$dbpassword = "dbpassword";
$dbname = "mydb";

$con = mysql_connect($dbhostname,$dbusername,$dbpassword) ;
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db($dbname, $con);

$sql = "select uname,upwd,uemail from users where delflag=0 and inactive=0 and usertype in (3, 30)";
//echo $sql;

$result = mysql_query($sql);

while($row = mysql_fetch_array($result)) {

$username = $row[uname];
$password = $row[upwd];
$email = $row[uemail];

// Add the users to vBulletin
$newuserid = register_in_vb($username, $password, $email);

// echo "hi ".$newuserid;

if(!$newuserid) {
echo "hello no newuserid";
}

}// end while
mysql_close($con);

?>

kh99
07-19-2011, 10:58 PM
The "That username is already in use..." message probably means the username is already in the database (or it could mean it's a username that isn't allowed), so up to there it looks like things are working as expected. I think you're getting the "Fatal error: Registry object is not an object ..." because the code is depending on including init.php to define $vbulletin, but you've got require_once() so it only works once.

To be honest, I looked at it trying to see how to fix it but I can't understand what's going on. Do you understand why it seems to be trying to replace the existing $vbulletin with a new one?