LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-11-2012, 03:38 PM   #1
czezz
Member
 
Registered: Nov 2004
Location: Poland/Warsaw
Distribution: Slackware/Solaris
Posts: 438

Rep: Reputation: 30
PHP and 2x connection to DB


In PHP code I open 2 connection to 2 different databases (on different hosts).


PHP Code:
$dbh1 mysql_connect($hostname$username$password) or die(mysql_error()); 
mysql_select_db('database1'$dbh1);
mysql_query('select * from tablename'$dbh1);

/*
and for database 2
*/

$dbh2 mysql_connect($hostname$username$password)  or die(mysql_error());
mysql_select_db('database2'$dbh2);
mysql_query('select * from tablename'$dbh2); 
That works just fine.
However, if connection to first DB - $dbh1 - fails, then I can see only error and no data form $dbh2.

Actually nothing what is below $dbh1 is displayed. Even test message like: print "test";

Why is it like this and what to do to make it works?
My system details:
Code:
Apache/2.2.17 (Unix) DAV/2 PHP/5.3.6 
mysql  Ver 14.14 Distrib 5.1.56, for slackware-linux-gnu (i486) using readline 5.1

Last edited by czezz; 02-11-2012 at 03:39 PM.
 
Old 02-11-2012, 04:08 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
The reason you see nothing else is because you told PHP to not do anything else.

Your command:
Code:
$dbh1 = mysql_connect($hostname, $username, $password) or die(mysql_error());
So, if the mysql_connect() fails, then the die() function is called--but ONLY if the mysql_connect() fails.

The documentation for die() states that it is equivalent to exit(). And the documentation for exit() says that the script will terminate after such a call.

If you want to continue your script regardless of whether either mysql_connect() function fails, then you need to remove the "or die()" portions and insert checks to make sure you have a valid, working connection before you submit queries.
 
Old 02-11-2012, 04:39 PM   #3
czezz
Member
 
Registered: Nov 2004
Location: Poland/Warsaw
Distribution: Slackware/Solaris
Posts: 438

Original Poster
Rep: Reputation: 30
This is correct. When I remove die() it works.
And I have added validation:

PHP Code:
$dbh1 mysql_connect($hostname$username$password)

if(empty(
$dbh1))
  {
   print 
$c_ekt;
   print (
"Can not establish connection to DB " mysql_error());
  } 
TY for reply.

Last edited by czezz; 02-11-2012 at 04:51 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mssql connection in php kalidoss Linux - Software 3 10-26-2009 05:53 AM
PHP connection refused Charlieasmith Linux - General 1 01-12-2006 03:12 PM
php-mysql connection pb abd_bela Debian 2 09-23-2005 04:07 AM
connection from one php script to another ankscorek Linux - Software 6 05-03-2005 04:33 AM
SSH Connection via PHP? basemodel Programming 2 08-27-2004 08:26 PM


All times are GMT -5. The time now is 06:41 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration