FORUM HOME | WHUK BLOG   
WEB HOSTING UK AFFORDABLE WEBSITE HOSTING SERVICES IN UNITED KINGDOM
PHP LINUX SHARED HOSTING WINDOWS ASP.NET HOSTING PACKAGES
ECOMMERCE HOSTING ASP MSSQL MS ACCESS ODBC FRONTPAGE HOSTING
CPANEL WHM FANTASTICO RESELLER DEDICATED SERVER WEB HOSTING
CHEAP PLESK CPANEL HTML MYSQL BEST UK VPS HOSTING COMPANY
CHEAP RELIABLE UK HOSTING PROVIDER SINCE 2001
MANAGED WEB HOSTING SERVICE
AFFORDABLE WEBSITE HOSTING SERVICES IN UNITED KINGDOM

Web Hosting UK Forums | Linux Windows Dedicated Server and cPanel VPS Hosting Forum » Technical Support » MySQL Hosting

Reply
 
LinkBack Thread Tools Display Modes

  #1 (permalink)  
Old 01-12-09, 12:10 AM
Senior Member
 
Join Date: Dec 2006
Posts: 684
Send a message via MSN to karimali831
Default Connect to two databases?

Is this possible? I want to connect to two databases from one php page. Both databases are on the same machine also. I use a CMS and want it to read a group of tables from db1, and another group of tables frmo db2.

any help much appreciated

thank you
Reply With Quote

  #2 (permalink)  
Old 01-12-09, 05:11 PM
sysadmin's Avatar
Administrator
 
Join Date: Oct 2006
Posts: 296
Smile

Quote:
Originally Posted by karimali831 View Post
Is this possible? I want to connect to two databases from one php page. Both databases are on the same machine also. I use a CMS and want it to read a group of tables from db1, and another group of tables frmo db2.

any help much appreciated

thank you
Hi Karim,

If you are trying to open multiple, separate MySQL connections with the same MySQL user, password, & hostname/IP, you must set $new_link = TRUE to prevent mysql_connect from using an existing connection. This parameter has been available since PHP 4.2.0 and allows you to open a new link even if the call uses the same parameters.

For example, you are opening two separate connections to two different databases (but on the same host, & with the same user & password):

PHP Code:
$db1 mysql_connect($dbhost$dbuser$dbpass);
$rv mysql_select_db($dbname1$db1);
$db2 mysql_connect($dbhost$dbuser$dbpass);
$rv mysql_select_db($dbname2$db2); 
At this point, both $db1 & $db2 will have selected the database named by $dbname2.

The workaround is to require that the second MySQL connection is new:

PHP Code:
$db1 mysql_connect($dbhost$dbuser$dbpass);
$rv mysql_select_db($dbname1$db1);
$db2 mysql_connect($dbhost$dbuser$dbpassTRUE);
$rv mysql_select_db($dbname2$db2); 
Now, $db1 should have selected $dbname1, & $db2 should have selected $dbname2.

ie: by calling mysql_select_db($database) before every SQL query just to be sure which database you are working with.

Note: This occurs only when the server, username, & password parameters are identical for each mysql_connect statement..
__________________
Regards,
Jack Daniel.

Cloud Hosting || Managed Dedicated Server || Webhosting UK Knowledgebase
Reply With Quote

  #3 (permalink)  
Old 01-12-09, 09:02 PM
Senior Member
 
Join Date: Dec 2006
Posts: 684
Send a message via MSN to karimali831
Default

I'm always confused with this SQL stuff but I hope you understand this. From the CMS, the database info is like:

Code:
<?php
 $host = "localhost";
 $user = "";
 $pwd = "";
 $db = "";

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.'"');
With the code you provided in your post can you help me add it to make the complete above code if I'm making sense or do you require more info?

Help much appreciated, thank you!
Reply With Quote

  #4 (permalink)  
Old 02-12-09, 02:38 PM
sysadmin's Avatar
Administrator
 
Join Date: Oct 2006
Posts: 296
Smile

Quote:
Originally Posted by karimali831 View Post
I'm always confused with this SQL stuff but I hope you understand this. From the CMS, the database info is like:

Code:
<?php
 $host = "localhost";
 $user = "";
 $pwd = "";
 $db = "";

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.'"');
With the code you provided in your post can you help me add it to make the complete above code if I'm making sense or do you require more info?

Help much appreciated, thank you!
Try this:


PHP Code:
// common host because both db on the same  server
$host "localhost";

// User & pass for first database
$user1 =  "";
$pwd1 "";
$db1 "";

// User & pass for second database
$user2 =  "";
$pwd2 "";
$db2 "";

// Connect to first Database and get link for  connection
$link1 mysql_connect($host$user1$pwd1) or  system_error('ERROR: Can not connect to MySQL-Server');

//Select First  
mysql_select_db($db1,$link1) or system_error('ERROR: Can not connect to  database "'.$db1.'"');

// Connect to second Database and get link for  connection
$link2 mysql_connect($host$user2$pwd2) or  system_error('ERROR: Can not connect to MySQL-Server');
//Select First  
mysql_select_db($db2,$link2) or system_error('ERROR: Can not connect to  database "'.$db2.'"');

//Select query for first database
$result =  mysql_query("select * from db1",$link1);

//Select query for second database
$result =  mysql_query("select * from db2",$link2); 
__________________
Regards,
Jack Daniel.

Cloud Hosting || Managed Dedicated Server || Webhosting UK Knowledgebase
Reply With Quote

  #5 (permalink)  
Old 02-12-09, 09:28 PM
Senior Member
 
Join Date: Dec 2006
Posts: 684
Send a message via MSN to karimali831
Default

Just what I was after
You made a great ending to my day... thank you!
Reply With Quote

  #6 (permalink)  
Old 04-12-09, 03:41 PM
sysadmin's Avatar
Administrator
 
Join Date: Oct 2006
Posts: 296
Wink

Quote:
Originally Posted by karimali831 View Post
Just what I was after
You made a great ending to my day... thank you!
You're welcome
__________________
Regards,
Jack Daniel.

Cloud Hosting || Managed Dedicated Server || Webhosting UK Knowledgebase
Reply With Quote

  #7 (permalink)  
Old 16-05-10, 12:00 AM
new member
 
Join Date: Apr 2010
Posts: 9
Default

This thread just greatly helped me on some issues I was having. Thanks!
Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT. The time now is 07:09 AM.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Copyright 2001-2010 Web Hosting UK. All rights reserved.
Web Hosting UK Forum





Site Map

Shared Cloud
Shared Cloud From £1

Affiliate Program
Earn up to £300 Per Sale

Dedicated Servers
Dedicated Server Hosting

Cloud Hosting
Cloud Server Hosting

Load Balanced Server
Load Balancing Server

VPS Hosting
Linux VPS Hosting

Windows VPS
Windows 2003 VPS

Zimbra Hosting
Zimbra Email Hosting

cPanel Hosting
Shared Linux Hosting

Windows Hosting
Shared Windows Hosting

Coldfusion Hosting
Windows Coldfusion Hosting

cPanel Reseller Hosting
Reseller Hosting

Windows Reseller
Windows Reseller Hosting

Email Web Hosting
Email Hosting

Semi-Dedicated Server
Semi-Dedicated Hosting

Remote Backup Plans
Offsite Backup Service


cpanel hosting
Knowledgebase Articles

Pre-Sales Question
Web Hosting FAQ's

Dedicated Hosting
Dedicated Server FAQ's

Virtual Private Servers
VPS Hosting

PHP MySQL Hosting
cPanel Hosting

Windows Hosting
ASP MSSQL Hosting

Domain Name
Domain registration FAQ's

CMS Hosting
CMS Hosting FAQ's

Payment Gateways
Payment FAQ's


Support Tutorials

cPanel Tutorials
cPanel Flash Tutorials

Wordpress Tutorials
Wordpress Flash Tutorials

Plesk Tutorials
Plesk Flash Tutorials

PhpMyadmin Tutorials
PhpMyadmin Flash Tutorials

Drupal Tutorials
Drupal Flash Tutorials

Mambo Tutorials
Mambo Flash Tutorials

Joomla Tutorials
Joomla Flash Tutorials

More Hosting Tutorials