problem connecting with 2 databases

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

problem connecting with 2 databases

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: problem connecting with 2 databases

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: problem connecting with 2 databases

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply