Page 1 of 1

Multiple MySQL connections

Posted: Mon Nov 26, 2007 9:22 pm
by tristanlee85
I have to be doing something wrong here because I am getting a lot of errors. I'm using phpBB and modding it to connect to another database which requires me to use another username and password. The problem I am having is I am creating another connection:

Code: Select all

// Insert the coupon code into the store database //
		$coupon_conn = mysql_connect("localhost", "user2", "pass2");
                @mysql_select_db("web11_db4") or die(mysql_error());

                $coupon_sql = "INSERT INTO xcart_discount_coupons VALUES ('".$coupon_code."', '".$discount_value."', 'absolute', '0', '0', '10.00', '1', 'N', '0', '1230699600', 'A', 'master', 'N', 'N', 'N')";
                mysql_query($coupon_sql);
                mysql_close($coupon_conn);
yet anything after that code that needs to insert into the current database phpBB uses doesn't insert. I'm pretty sure that's either closing all connections or my code afterwards is trying to still use that database. Here is everything if it helps:

Code: Select all

<?php

if ($userdata['session_logged_in'])
{
	$sql = "SELECT * FROM phpbb_users WHERE username = '" . $userdata['username'] . "' LIMIT 1";
	
	$posts =  mysql_result(mysql_query($sql),0,"user_posts");
	$coupon_code = mysql_result(mysql_query($sql),0,"coupon_code");
	$coupon_active = mysql_result(mysql_query($sql),0,"coupon_active");
	$coupon_value = "10.00";
	$discount_value = "10.00";
	
	if ($posts > 2 && !empty($coupon_code) && $coupon_active == '1')
	{
		// Insert the coupon code into the store database //
		$coupon_conn = mysql_connect("localhost", "user2", "pass2");
                @mysql_select_db("web11_db4") or die(mysql_error());

                $coupon_sql = "INSERT INTO xcart_discount_coupons VALUES ('".$coupon_code."', '".$discount_value."', 'absolute', '0', '0', '10.00', '1', 'N', '0', '1230699600', 'A', 'master', 'N', 'N', 'N')";
                mysql_query($coupon_sql);
                mysql_close($coupon_conn);
                
                // Set the coupon to inactive so we don't insert it again //
                $sql = "UPDATE phpbb_users SET coupon_active = '2' WHERE username='" . $userdata['username'] . "'";
                echo $sql;
                mysql_query($sql);
                
                // Send the user a PM stating that they are eligable for thir discount //
                $sql = "UPDATE phpbb_users SET user_new_privmsg = '1', user_last_privmsg = '9999999999' WHERE username = '" . $userdata['username'] . "'";

		if ( !($result = $db->sql_query($sql)) )

        {

		    message_die(GENERAL_ERROR, 'Could not update users table', '', __LINE__, __FILE__, $sql);

        }



        $register_pm_subject = "Store Coupon Active";

        $register_pm = $userdata['username'] . ",<br /><br />I just wanted to inform you that you have reached 25 posts. What does this mean to you? When you registered, you were given a coupon code that could be used in our <a href='http://store.plastikracing.net' target='_blank'>on-line store</a>. Now that you've reached 25 posts, this coupon is now active and available for you to use toward your next purchase. Remember, this coupon can only be used one time.<br /><br />If you have any questions, please <a href='privmsg.php?mode=post&u=2'>PM the administrator.<a/><br /><br />For your reference, you coupon code is: <b>" . $coupon_code . "</b>";

        $privmsgs_date = date("U");

        $sql = "INSERT INTO " . PRIVMSGS_TABLE . " (privmsgs_type, privmsgs_subject, privmsgs_from_userid, privmsgs_to_userid, privmsgs_date, privmsgs_enable_html, privmsgs_enable_bbcode, privmsgs_enable_smilies, privmsgs_attach_sig) VALUES ('0', '" . str_replace("\'", "''", addslashes(sprintf($register_pm_subject,$board_config['sitename']))) . "', '2', " . $user_id . ", " . $privmsgs_date . ", '0', '1', '1', '0')";

        if ( !$db->sql_query($sql) )

		{

			message_die(GENERAL_ERROR, 'Could not insert private message sent info', '', __LINE__, __FILE__, $sql);

		}



		$privmsg_sent_id = $db->sql_nextid();

		$privmsgs_text = "Store Coupon Active";
		

        $sql = "INSERT INTO " . PRIVMSGS_TEXT_TABLE . " (privmsgs_text_id, privmsgs_text) VALUES ($privmsg_sent_id, '" . str_replace("\'", "''", addslashes(sprintf($register_pm,$board_config['sitename'],$board_config['sitename']))) . "')";

        if ( !$db->sql_query($sql) )

		{

			message_die(GENERAL_ERROR, 'Could not insert private message sent text', '', __LINE__, __FILE__, $sql);

		}
	}	
}

?>

Posted: Mon Nov 26, 2007 10:15 pm
by s.dot
Use the second parameter of mysql_query() to tell the query which database connection you want to use.