karimali831 Posted February 6, 2011 Share Posted February 6, 2011 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? 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. Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/ Share on other sites More sharing options...
karimali831 Posted February 6, 2011 Author Share Posted February 6, 2011 can I still do this? Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/#findComment-1170645 Share on other sites More sharing options...
BlueSkyIS Posted February 6, 2011 Share Posted February 6, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/#findComment-1170665 Share on other sites More sharing options...
karimali831 Posted February 6, 2011 Author Share Posted February 6, 2011 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/#findComment-1170715 Share on other sites More sharing options...
BlueSkyIS Posted February 6, 2011 Share Posted February 6, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/#findComment-1170717 Share on other sites More sharing options...
BlueSkyIS Posted February 6, 2011 Share Posted February 6, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/#findComment-1170718 Share on other sites More sharing options...
karimali831 Posted February 6, 2011 Author Share Posted February 6, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/#findComment-1170723 Share on other sites More sharing options...
BlueSkyIS Posted February 6, 2011 Share Posted February 6, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/#findComment-1170725 Share on other sites More sharing options...
BlueSkyIS Posted February 6, 2011 Share Posted February 6, 2011 and what is safe_query()? does it do anything if the SQL query fails? Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/#findComment-1170729 Share on other sites More sharing options...
karimali831 Posted February 6, 2011 Author Share Posted February 6, 2011 No safe query is fine, it just like mysql_query but safe manner I think. So... still not working? 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/#findComment-1170732 Share on other sites More sharing options...
BlueSkyIS Posted February 6, 2011 Share Posted February 6, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/#findComment-1170735 Share on other sites More sharing options...
BlueSkyIS Posted February 6, 2011 Share Posted February 6, 2011 an obvious question: is there more than one record in PREFIX."user"? Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/#findComment-1170736 Share on other sites More sharing options...
karimali831 Posted February 6, 2011 Author Share Posted February 6, 2011 It works with mysql_query() !! 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 Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/#findComment-1170738 Share on other sites More sharing options...
BlueSkyIS Posted February 6, 2011 Share Posted February 6, 2011 What's wrong with safe_query then? don't know. never seen it before and don't know why it's doing what it's doing. Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/#findComment-1170747 Share on other sites More sharing options...
karimali831 Posted February 6, 2011 Author Share Posted February 6, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/#findComment-1170749 Share on other sites More sharing options...
BlueSkyIS Posted February 6, 2011 Share Posted February 6, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/#findComment-1170751 Share on other sites More sharing options...
Pikachu2000 Posted February 6, 2011 Share Posted February 6, 2011 MD5, along with all other hashing algorithms, cannot be undone. Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/#findComment-1170758 Share on other sites More sharing options...
karimali831 Posted February 6, 2011 Author Share Posted February 6, 2011 ok luckily phpBB supports MD5 anyway so it's all good. I inserted all users from CMS to phpBB forum and login works fine now on both systems, just need to work around session sync, thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/226853-loop-to-insert-multiple-rows-multiple-mysql-connections-help-please/#findComment-1170762 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.