Page 1 of 1

problem connecting with 2 databases

Posted: Fri May 20, 2011 6:36 pm
by nite4000
Hey I need some help with using 2 databases

this is for a script to chk the licensing what needs to happen is when a user installs the script it creates a file with the db info in it then at the end it generates a code and I need to connect back to the main site where the script was purchased from and insert the info into the table.

Here is my code so far

Code: Select all

  <?PHP
require_once('db.php');
$dbc = db_connect();
require_once('db_license.php');
$dbc2 = db_connect2();
require_once('random_ticket_id.php');
$ticketId = generateTicketId ();
?>
$dbc is for the file that was created
$dbc2 is the file used to connect to the main site

thats no problem however I need to insert the info into a table related to $dbc2 i have tried different ways like

Code: Select all

$r= "INSERT into table(id,domain)VALUES(null,'{$var['db_field']}')",$dbc2;
but nothing works i also need to select from a table from the users site with $dbc i tried this for that...

Code: Select all

  $q = mysql_query("SELECT * FROM table WHERE id='1', $dbc1") or die(mysql_error());
 $var= mysql_fetch_array($q, MYSQL_ASSOC);
@mysql_free_result($q);

the problem is getting the querys to work with selecting and insertings from both databases


any help would be nice.

thanks

Re: problem connecting with 2 databases

Posted: Sun May 22, 2011 7:54 am
by social_experiment
From your code you must be using mysql_select_db() when selecting the database and the last database that you select is used for all queries there after.
The Manual wrote:Sets the current active database on the server that's associated with the specified link identifier. Every subsequent call to mysql_query() will be made on the active database.
I'm thinking you will have to select the database before you make each of the queries, that way the correct database is selected.

Re: problem connecting with 2 databases

Posted: Thu May 26, 2011 2:25 pm
by pickle

Code: Select all

$r= "INSERT into table(id,domain)VALUES(null,'{$var['db_field']}')",$dbc2;
This will concatenate $dbc2 to your query, which will do nothing but break your query.

What do db_connect() and db_connect2() return? I assume a resource that is a connection to the relevant database? If so, just pass $dbc and $dbc2 to mysql_query() to run queries on the associated databases.