Jump to content

Loop to insert multiple rows (multiple mysql connections??) help please!


Recommended Posts

Ok I'm trying to insert multiple rows by using a while loop but having problems. At the same time, need to open a new mysql connection while running the insert query, close it then open the previous mysql connection.

 

I managed to insert multiple queries before using a loop, but for this time, the loop does not work?  :confused::shrug:

I think it is because I am opening another connection... yh that would make sense actually?

 

Here is the code:

 

$users = safe_query("SELECT * FROM ".PREFIX."user");
       while($dp=mysql_fetch_array($users)) { 
          
		 $username = $dp['username'];
	 $nickname = $dp['nickname'];
	 $pwd1 = $dp['password']; 
	 $mail = $dp['email'];
	 $ip_add = $dp['ip'];
	 $wsID = $dp['userID'];
	 $registerdate = $dp['registerdate'];
	 $birthday = $dp['birthday'];
	 $avatar = $dp['avatar'];
	 $icq = $dp['icq']; 
	 $hp = $dp['homepage'];	

		echo $username." = 1 username only? ";
      

		// ----- Forum Bridge user insert -----

		$result = safe_query("SELECT * FROM `".PREFIX."forum`");
		$ds=mysql_fetch_array($result);
		$forum_prefix = $ds['prefix'];
		define(PREFIX_FORUM, $forum_prefix);
		define(FORUMREG_DEBUG, 0);

			$con = mysql_connect($ds['host'], $ds['user'], $ds['password']) or system_error('ERROR: Can not connect to MySQL-Server');
			$condb = mysql_select_db($ds['db'], $con) or system_error('ERROR: Can not connect to database "'.$ds['db'].'"');
	                
					include('../_phpbb_func.php');
					$phpbbpass = phpbb_hash($pwd1);
					$phpbbmailhash = phpbb_email_hash($mail);
					$phpbbsalt = unique_id();

					safe_query("INSERT INTO `".PREFIX_FORUM."users` (`username`, `username_clean`, `user_password`, `user_pass_convert`, `user_email`, `user_email_hash`, `group_id`, `user_type`, `user_regdate`, `user_passchg`, `user_lastvisit`, `user_lastmark`, `user_new`, `user_options`, `user_form_salt`, `user_ip`, `wsID`, `user_birthday`, `user_avatar`, `user_icq`, `user_website`) VALUES ('$username', '$username', '$phpbbpass', '0', '$mail', '$phpbbmailhash', '2', '0', '$registerdate', '$registerdate', '$registerdate', '$registerdate', '1', '230271', '$phpbbsalt', '$ip_add', '$wsID', '$birthday', '$avatar', '$icq', '$hp')");
					if (FORUMREG_DEBUG == '1') {
						echo "<p><b>-- DEBUG -- : User added: ".mysql_affected_rows($con)."<br />";
      						echo "<br />-- DEBUG -- : Query used: ".end($_mysql_querys)."</b></p><br />";
      					
      					$result = safe_query("SELECT user_id from ".PREFIX_FORUM."users WHERE username = '$username'");
      					$phpbbid = mysql_fetch_row($result);
					safe_query("INSERT INTO `".PREFIX_FORUM."user_group` (`group_id`, `user_id`, `group_leader`, `user_pending`) VALUES ('2', '$phpbbid[0]', '0', '0')");
					safe_query("INSERT INTO `".PREFIX_FORUM."user_group` (`group_id`, `user_id`, `group_leader`, `user_pending`) VALUES ('7', '$phpbbid[0]', '0', '0')");
					mysql_close($con);
		}

        		include('../_mysql.php');        		
        		mysql_connect($host, $user, $pwd) or system_error('ERROR: Can not connect to MySQL-Server');
			mysql_select_db($db) or system_error('ERROR: Can not connect to database "'.$db.'"');
       
       }

 

So I need to be able to insert these rows using the while loop.. how can I do this?

 

I really appreciate any help.

open and close connections outside of the loop and use the same connections within the loop.

 

mysql_connect() returns a resource identifier that you can use to indicate which connection you are querying

 

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

I have merged databases so now I use one connection, but still while loop is not working...

where I echo $username, there should be many outputs for this but there is only 1.

 

what could be the reason loop is not looping?

 

   $users = safe_query("SELECT * FROM ".PREFIX."user WHERE activated='1'");
       while($dp=mysql_fetch_array($users)) { 
          
		 $username = $dp['username'];
	 $nickname = $dp['nickname'];
	 $pwd1 = $dp['password']; 
	 $mail = $dp['email'];
	 $ip_add = $dp['ip'];
	 $wsID = $dp['userID'];
	 $registerdate = $dp['registerdate'];
	 $birthday = $dp['birthday'];
	 $avatar = $dp['avatar'];
	 $icq = $dp['icq']; 
	 $hp = $dp['homepage'];	

	 echo $username."<br>";

		// ----- Forum Bridge user insert -----

		$result = safe_query("SELECT * FROM `".PREFIX."forum`");
		$ds=mysql_fetch_array($result);
		$forum_prefix = $ds['prefix'];
		define(PREFIX_FORUM, $forum_prefix);
		define(FORUMREG_DEBUG, 0);

					include('../_phpbb_func.php');
					$phpbbpass = phpbb_hash($pwd1);
					$phpbbmailhash = phpbb_email_hash($mail);
					$phpbbsalt = unique_id();

					safe_query("INSERT INTO `".PREFIX_FORUM."users` (`username`, `username_clean`, `user_password`, `user_pass_convert`, `user_email`, `user_email_hash`, `group_id`, `user_type`, `user_regdate`, `user_passchg`, `user_lastvisit`, `user_lastmark`, `user_new`, `user_options`, `user_form_salt`, `user_ip`, `wsID`, `user_birthday`, `user_avatar`, `user_icq`, `user_website`) VALUES ('$username', '$username', '$phpbbpass', '0', '$mail', '$phpbbmailhash', '2', '0', '$registerdate', '$registerdate', '$registerdate', '$registerdate', '1', '230271', '$phpbbsalt', '$ip_add', '$wsID', '$birthday', '$avatar', '$icq', '$hp')");

      					$result = safe_query("SELECT user_id from ".PREFIX_FORUM."users WHERE username = '$username'");
      					$phpbbid = mysql_fetch_row($result);
					safe_query("INSERT INTO `".PREFIX_FORUM."user_group` (`group_id`, `user_id`, `group_leader`, `user_pending`) VALUES ('2', '$phpbbid[0]', '0', '0')");
					safe_query("INSERT INTO `".PREFIX_FORUM."user_group` (`group_id`, `user_id`, `group_leader`, `user_pending`) VALUES ('7', '$phpbbid[0]', '0', '0')");

					echo 'Success';

		}

this line should be before the loop, or changed to include_once():

 

include('../_phpbb_func.php');

 

do you have error_reporting turned on full? if not (or if you don't know), i suggest that you put this at the top of your PHP:

 

error_reporting(E_ALL);
ini_set("display_errors", -1);

I have done what you both said, but loop is still not happy:

 

error_reporting(E_ALL);
ini_set("display_errors", -1);
include_once('../_phpbb_func.php');

   $users = safe_query("SELECT * FROM ".PREFIX."user WHERE activated='1'");
       while($dp=mysql_fetch_array($users)) { 
          
		 $username = $dp['username'];
	 $nickname = $dp['nickname'];
	 $pwd1 = $dp['password']; 
	 $mail = $dp['email'];
	 $ip_add = $dp['ip'];
	 $wsID = $dp['userID'];
	 $registerdate = $dp['registerdate'];
	 $birthday = $dp['birthday'];
	 $avatar = $dp['avatar'];
	 $icq = $dp['icq']; 
	 $hp = $dp['homepage'];	

	 echo $username."<br>";

		// ----- Forum Bridge user insert -----

		$result = safe_query("SELECT * FROM `".PREFIX."forum`");
		$ds=mysql_fetch_array($result);
		$forum_prefix = $ds['prefix'];

					$phpbbpass = phpbb_hash($pwd1);
					$phpbbmailhash = phpbb_email_hash($mail);
					$phpbbsalt = unique_id();

					safe_query("INSERT INTO `".$forum_prefix."users` (`username`, `username_clean`, `user_password`, `user_pass_convert`, `user_email`, `user_email_hash`, `group_id`, `user_type`, `user_regdate`, `user_passchg`, `user_lastvisit`, `user_lastmark`, `user_new`, `user_options`, `user_form_salt`, `user_ip`, `wsID`, `user_birthday`, `user_avatar`, `user_icq`, `user_website`) VALUES ('$username', '$username', '$phpbbpass', '0', '$mail', '$phpbbmailhash', '2', '0', '$registerdate', '$registerdate', '$registerdate', '$registerdate', '1', '230271', '$phpbbsalt', '$ip_add', '$wsID', '$birthday', '$avatar', '$icq', '$hp')");

      					$result = safe_query("SELECT user_id from ".PREFIX_FORUM."users WHERE username = '$username'");
      					$phpbbid = mysql_fetch_row($result);
					safe_query("INSERT INTO `".PREFIX_FORUM."user_group` (`group_id`, `user_id`, `group_leader`, `user_pending`) VALUES ('2', '$phpbbid[0]', '0', '0')");
					safe_query("INSERT INTO `".PREFIX_FORUM."user_group` (`group_id`, `user_id`, `group_leader`, `user_pending`) VALUES ('7', '$phpbbid[0]', '0', '0')");

					echo 'Success';

		}

 

Only 1 output from loop.. :(

 

Thanks for helping!!..

P.S. No errors.

this line should be before the loop, or changed to include_once():

 

include('../_phpbb_func.php');

defining and re-defining these constants probably isn't helping anything. i would just use regular variables

 

		define(PREFIX_FORUM, $forum_prefix);
		define(FORUMREG_DEBUG, 0);

 

 

No safe query is fine, it just like mysql_query but safe manner I think.

 

So... still not working?  :shrug:

 

error_reporting(E_ALL);
ini_set("display_errors", -1);
include('../_phpbb_func.php');

   $users = safe_query("SELECT * FROM ".PREFIX."user WHERE activated='1'");
       while($dp=mysql_fetch_array($users)) { 
          
		 $username = $dp['username'];
	 $nickname = $dp['nickname'];
	 $pwd1 = $dp['password']; 
	 $mail = $dp['email'];
	 $ip_add = $dp['ip'];
	 $wsID = $dp['userID'];
	 $registerdate = $dp['registerdate'];
	 $birthday = $dp['birthday'];
	 $avatar = $dp['avatar'];
	 $icq = $dp['icq']; 
	 $hp = $dp['homepage'];	

	 echo $username."<br>";

		// ----- Forum Bridge user insert -----

					$phpbbpass = phpbb_hash($pwd1);
					$phpbbmailhash = phpbb_email_hash($mail);
					$phpbbsalt = unique_id();

					safe_query("INSERT INTO `phpbb_users` (`username`, `username_clean`, `user_password`, `user_pass_convert`, `user_email`, `user_email_hash`, `group_id`, `user_type`, `user_regdate`, `user_passchg`, `user_lastvisit`, `user_lastmark`, `user_new`, `user_options`, `user_form_salt`, `user_ip`, `wsID`, `user_birthday`, `user_avatar`, `user_icq`, `user_website`) VALUES ('$username', '$username', '$phpbbpass', '0', '$mail', '$phpbbmailhash', '2', '0', '$registerdate', '$registerdate', '$registerdate', '$registerdate', '1', '230271', '$phpbbsalt', '$ip_add', '$wsID', '$birthday', '$avatar', '$icq', '$hp')");

      					$result = safe_query("SELECT user_id from ".PREFIX_FORUM."users WHERE username = '$username'");
      					$phpbbid = mysql_fetch_row($result);
					safe_query("INSERT INTO `".PREFIX_FORUM."user_group` (`group_id`, `user_id`, `group_leader`, `user_pending`) VALUES ('2', '$phpbbid[0]', '0', '0')");
					safe_query("INSERT INTO `".PREFIX_FORUM."user_group` (`group_id`, `user_id`, `group_leader`, `user_pending`) VALUES ('7', '$phpbbid[0]', '0', '0')");

					echo 'Success';

		}

No safe query is fine, it just like mysql_query but safe manner I think.

 

how do you know if the SQL query fails? I suggest that you replace safe_query with regular old mysql_query, and check for errors.

 

$result = msyql_query($sql) or die(mysql_error());

 

or update safe_query so that it uses mysql_error() if the query fails.

It works with mysql_query() :D!!

 

safe_query(): 1 record

mysql_query(): hundreds

 

What's wrong with safe_query then?

 

function safe_query($query="") {
global $_mysql_querys;
if(stristr(str_replace(' ', '', $query), "unionselect")===FALSE AND stristr(str_replace(' ', '', $query), "union(select")===FALSE){
	$_mysql_querys[] = $query;
	if(empty($query)) return false;
	if(DEBUG == "OFF") $result = mysql_query($query) or die('Query failed!');
	else {
		$result = mysql_query($query) or die('Query failed: '
		.'<li>errorno='.mysql_errno()
		.'<li>error='.mysql_error()
		.'<li>query='.$query);
	}
	return $result;
}
else die();
}

 

 

I

I just have problem with password now.

 

I have phpBB and WS in one database, both uses different password hashes.

CMS uses MD5 encryption, phpBB god knows.

 

I've got phpbb function to get password hash, but for my sake, it will hash all the MD5 passwords which is no good. :(

Somehow I need to decrypt MD5 then use the MD5 decrypt to get that hashed.

 

Anyone understand? If not please say, also if this is not possible, any alternative?

are you trying to log in to wp and phpbb at the same time? if so, that can be done without messing with the hashes. both systems provide login functionality that can be used to log in to the system from another system.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.