Multiple connections

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Multiple connections

Post by psmshankar »

Hi,

I am having a weird probs..
I use the following code in a file (connection.php) and executed from my machine(localhost). The MySQL database resides in another server (10.100.120.1)
$username = '';
$password = '';
$dbname_one = "testA";
$dbname_two = "testB";
$connection_one = mysql_connect("10.100.120.1",$username,$password,true) or die ("Could not connect connection one");
$connection_two = mysql_connect("10.100.120.1",$username,$password,true) or die ("Could not connect connection two");

mysql_select_db($dbname_one,$connection_one);
mysql_select_db($dbname_two,$connection_two);

echo "Connection ok..";
When i execute, it works perfectly and able to connect to two databases same time....
But when i run the same file from Server(10.100.120.1), it says
Cound not connect to connection one...
and stops...

What could be the problem?

My machine(localhost) - MySQL 3.23/PHP 4.0.6/Apache 1.39/Win98
Server(10.100.120.1) - MySQL 3.23/PHP 4.0.6/Apache 1.39/Win2K

Is it any configuration i need to do???

Thanks
Shankar
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you set up a username and password for a user who can access the MySQL server from your IP address?

Mac
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Post by psmshankar »

No username and password....
how come that i can accees it from my PC, where as the same file when uploaded to server and run from server ..could not?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Because MySQL is probably setup so that any user (or no user) can access it from localhost. Create a user and give them a password and give them access rights to the database from your IP address.

Mac
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Post by psmshankar »

At present anybody can access it..
Do you mean i need to GRANT priveleges??
Its not from my machine....
I want that conenction file to be in server and run from server as user can access it from web...
We are running that server as web server.
In one of my program, I need to perform an operation for which i need to fetch data from two tables of different database...
so when i used that connection file in my program(program runs in server) it says "could not connect to connection_one... "
Last edited by psmshankar on Fri Oct 18, 2002 9:48 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You should never have a database that anybody can access from anywhere. Add users and passwords. Also telnet to your MySQL servers IP address and port number (normally 3306) to check that it is possible to do a remote connection and that firewalls or things like that aren't getting in the way.

Mac
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

do you use

Code: Select all

mysql_connect ( $host, $user, $password);
then the account $user should be granted access to the database with GRANT

read http://www.php.net/manual/en/function.mysql-connect.php to learn more about php defaults
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Post by psmshankar »

mac,
yeah at present we are not using any username or password..but will be doing it soon...
telnet to your MySQL servers IP address and port number (normally 3306) to check that it is possible to do a remote connection and that firewalls or things ...
i cannot able to telnet...but i say that i can run that connection file from my system connecting to MysQL database in server(10.100.120.1)
so it means that i can connect to that server, is it not????
therz no firewall...

volka,
i used mysql_connect ( $host, $user, $password);
i hav used a third parameter true value...means that i can hav multiple connection...
at present no username and password, so in my program
i used

Code: Select all

<?php
$username='';
$password='';
?>
here goes the full code...i am getting Modules, then Lessons for that modules, then Slides for that Lesson then checking whether the slides were already accessed or not...(this checking is done with data from a table of another database...

Code: Select all

<?php
<?
$username = '';
$password = '';
$dbname_one = "mmlsdb";
$dbname_two = "Student_Monitor";
$connection_one = mysql_connect("10.100.120.1",$username,$password,true) or die ("Could not connect connection one");
$connection_two = mysql_connect("10.100.120.1",$username,$password,true) or die ("Could not connect connection two");

mysql_select_db($dbname_one,$connection_one);
mysql_select_db($dbname_two,$connection_two);

echo "<br><b>connection ok...</b></br>";

$strSpace = str_repeat(" ",4);

$sSQL = "SELECT * FROM Module WHERE CoordinatorID=1034387138 ORDER BY ModuleID";
$ModuleResult=mysql_query($sSQL,$connection_one);

$ModCount=1;
while($rsModule=mysql_fetch_array($ModuleResult))
{
        $ModID = $rsModuleї"ModuleID"];
        $ModTitle = $rsModuleї"ModuleTitle"];
        $ModTitle = strtoupper($ModTitle);
        echo "<br><font color=green><b>$ModCount.   
 ModTitle</b></font>";

        $sSQL = "SELECT * FROM Chapter WHERE   
 CoordinatorID=1034387138 AND ModuleID=$ModID  
 ORDER BY ChapterID";
        $LessonResult=mysql_query($sSQL,$connection_one);

        $LessonCount=1;
        while($rsLesson=mysql_fetch_array($LessonResult))
        {
                $LessonID = $rsLessonї"ChapterID"];
                $LessonTitle = $rsLessonї"ChapterTitle"];
                echo "<br><b>$strSpace $LessonCount.  $LessonTitle</b>";

                $sSQL = "SELECT * FROM Slide WHERE 
                             CoordinatorID=1034387138 AND ChapterID=$LessonID AND ModuleID=$ModID ORDER BY Sequence";
                $SlideResult=mysql_query($sSQL,$connection_one);

                $SlideCount=1;
                while($rsSlide=mysql_fetch_array($SlideResult))
                {
                        $SlideID = $rsSlideї"SlideID"];
                        $SlideTitle = $rsSlideї"SlideTitle"];
                        echo "<br><b>$strSpace$strSpace $SlideCount.  $SlideTitle</b>";

                        $sSQL = "SELECT * FROM demoreport WHERE SlideID=$SlideID";
                        $VisitedResult=mysql_query($sSQL,$connection_two);
                        while($rsVisited=mysql_fetch_array($VisitedResult))
                        {
                                $Visited=$rsVisitedї"Visited"];
                        }

                        if($Visited==0)
                        {
                                echo "<font size='2' color=red><b>$strSpace Not Visited</b></font>";
                        }
                        else
                        {
                                echo "<font size='2' color=blue><b>$strSpace Visited</b></font>";
                        }

                        $SlideCount++;
                }//***END OF SLIDE LOOP

                $LessonCount++;
        }//***END OF LESSON LOOP

        $ModCount++;
        $LessonCount=1;
        $SlideCount=1;
}//***END OF MODULE LOOP

?>
1. This file(call it as test.php) i put in my machine 'Shankar' web directory. I am running Apache webserver locally.. so in my web browser i typed 'http://localhost/test.php' . so it works fine...it displays the modules, lessons, slides (visited or not visited)...see that the MySQL database is in server 10.100.120.1 ...i am getting perfectly...
2. Now actually this prgram should be in our server(10.100.120.1)
So i uploaded this program(test.php).Apache is runin on server...
so i typed http://10.100.120.1/test.php
herez the probs...i am not getting anything except
Could not connect connection one...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

the installation routine creates some default accounts for mysql (db accounts, not system accounts). That includes %@localhost so that every client that is accessing the database from the same machine as the mysql-server is granted access. You should alter that behavoiur and set passwords for all those default accounts (including 'root')

http://www.mysql.com/documentation/mysq ... l_security
http://www.mysql.com/documentation/mysq ... Management
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Post by psmshankar »

at present we hav no restrictions regd username and password...so no need to worry abut this now...(later we will)
lets say from 'start/run/' i type 'mysql -h 10.100.120.1 -u -p ',
it connects to MySQL database on 10.100.120.1 server.

we are already using that server for our web application
wherein we use connection files in the root directory.
so in our programs whre we need to use database connection we hav nclded this connection file...in connection file we just refer the host as localhost..this conenction files resides in server 10.100.120.1

our sample connection file

Code: Select all

<?php
<?
   $username = '';
   $password = '';

   $dbname = 'mmlsdb';

   $IPAddress="10.100.120.1";

   $link = mysql_connect("localhost",$username,$password) or die ("Could not connect");
   mysql_select_db($dbname) or die ("Could not select database");
?>

?>
so we could able to perform this...

Now my case is, i need to refer to two tables of different database....
so when i use
mysql_connect("localhost",'','',true) it says could not connect....the file i hav posted already

again i say , at present we dont have any restricts regd user....no user name and no password..
so what is the probs???

Volka & Mac , hope you guys understand what my probs is...
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Post by psmshankar »

hi guys...
i am going to hit the bed....its 2AM here sat morning...
Tom' i will check up ur replys..
bye
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

hmm...then you might want to know, what php/mysql think about why the connection failed

Code: Select all

$connection_one = mysql_connect("10.100.120.1",$username,$password,TRUE) or die ("Could not connect connection one :".mysql_error());
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Post by psmshankar »

Volka,
there is no error... i did what u hav said... still nothing...
it just displays
Could not connect connection one:
What is the problem???
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

that's very odd. It should have printed something like
Could not connect connection one :Can't connect to MySQL server on '10.100.120.1' (10060)
or something about invalid login/password - or what ever. But blank? 8O
Is there something in the webserver's error.log?
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Post by psmshankar »

i cannot get that
Can't connect to MySQL server on '10.100.120.1'
as i say from the begginning that i can connect to MySQL server on 10.100.120.1 hmmmmmmmmm

No...there is no error in the error log...
Post Reply