Not able to connect to MySQL with PHP

  • Follow


I installed the WAMP bundle with EasyPHP 5.3.3. Seems to be mostly 
working. At least I can create and display PHP pages in my localhost server.

I have defined (just for testing) a user in my SQL named "pubuser" and 
granted it access to a database "publications." Of course I also created 
the database and two tables.

I can access and manipulate the tables via phpMyAdmin and I can log in 
to sql using pubuser via the command-line interface.

I can also telnet to localhost port 3306 and I get something back before 
the port disconnects asfter a short while. THe return string looks like 
this:

 > B
 >  5.1.49-community-lo6IJyD%*☻L9wl8qR&cA[(

I have the following php code in a file named a login.php.

     <?php // login.php
     $db_hostname = 'localhost';
     $db_database = 'publications';
     $db_username = 'pubuser';
     $db_password = 'abc';
     ?>


also I have a test web page:

<html>
<head>
</head>
<body>
<?php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server)
  die("Unable to connect to MySQL: " . mysql_error());
  echo "Congrats, it seems you have connected to the server <br />
        host: $db_hostname<br />
        user: $db_username<br />
        password: $db_password <br  />
        for database: $db_database<br />";
  print_r($db_server);
  ?>
</body>
</html>

If I try to run this, the broser status message  briefly says 
"connecting to localhost" and then indefinitely it says "waiting for 
localhost..."

The browser window remains blank.

I have disabled my firewall, but the results remain the same.

If I comment out the connect statement, the "die" statement gets 
executed.  What should I look for?


Thanks
MikeB
0
Reply MPBrede (98) 9/21/2010 2:59:56 PM

On 9/21/2010 9:59 AM, MikeB wrote:

> I have the following php code in a file named a login.php.
>
> <?php // login.php
> $db_hostname = 'localhost';
> $db_database = 'publications';
> $db_username = 'pubuser';
> $db_password = 'abc';
> ?>
>
>
> also I have a test web page:
>
> <html>
> <head>
> </head>
> <body>
> <?php
> require_once 'login.php';
> $db_server = mysql_connect($db_hostname, $db_username, $db_password);


mysql_select_db($db_database);


> if (!$db_server)
> die("Unable to connect to MySQL: " . mysql_error());
> echo "Congrats, it seems you have connected to the server <br />
> host: $db_hostname<br />
> user: $db_username<br />
> password: $db_password <br />
> for database: $db_database<br />";
> print_r($db_server);
> ?>
> </body>
> </html>
>

I think that should do it.


-- 
TK ~ aka Terry Kimpling
http://wejuggle2.com/circusskills.php learn/make juggle/balance equipment
It ain't what they call you, it's what you answer to. W. C. Fields
0
Reply tknospa (34) 9/21/2010 4:15:04 PM


TK wrote:
> On 9/21/2010 9:59 AM, MikeB wrote:
>
>> I have the following php code in a file named a login.php.
>>
>> <?php // login.php
>> $db_hostname = 'localhost';
>> $db_database = 'publications';
>> $db_username = 'pubuser';
>> $db_password = 'abc';
>> ?>
>>
>>
>> also I have a test web page:
>>
>> <html>
>> <head>
>> </head>
>> <body>
>> <?php
>> require_once 'login.php';
>> $db_server = mysql_connect($db_hostname, $db_username, $db_password);
>
>
> mysql_select_db($db_database);
>
>
>> if (!$db_server)
>> die("Unable to connect to MySQL: " . mysql_error());
>> echo "Congrats, it seems you have connected to the server <br />
>> host: $db_hostname<br />
>> user: $db_username<br />
>> password: $db_password <br />
>> for database: $db_database<br />";
>> print_r($db_server);
>> ?>
>> </body>
>> </html>
>>
>
> I think that should do it.
>
>

Thanks, I know that the next step should be to actually select a 
database. However, the connect statement never completes, since I don't 
get the output after that, hence I don't believe that selecting a 
database will help.

Just for grins (and because I'm running out of ideas) I did try it and 
no surprise, it did not complete either.

  -MikeB


0
Reply MPBrede (98) 9/21/2010 4:49:03 PM

On 21-09-10 18:49, MikeB wrote:
> TK wrote:
>> On 9/21/2010 9:59 AM, MikeB wrote:
>>
>>> I have the following php code in a file named a login.php.
>>>
>>> <?php // login.php
>>> $db_hostname = 'localhost';
>>> $db_database = 'publications';
>>> $db_username = 'pubuser';
>>> $db_password = 'abc';
>>> ?>
>>>
>>>
>>> also I have a test web page:
>>>
>>> <html>
>>> <head>
>>> </head>
>>> <body>
>>> <?php
>>> require_once 'login.php';
>>> $db_server = mysql_connect($db_hostname, $db_username, $db_password);
>>
>>
>> mysql_select_db($db_database);
>>
>>
>>> if (!$db_server)
>>> die("Unable to connect to MySQL: " . mysql_error());
>>> echo "Congrats, it seems you have connected to the server <br />
>>> host: $db_hostname<br />
>>> user: $db_username<br />
>>> password: $db_password <br />
>>> for database: $db_database<br />";
>>> print_r($db_server);
>>> ?>
>>> </body>
>>> </html>
>>>
>>
>> I think that should do it.
>>
>>
>
> Thanks, I know that the next step should be to actually select a
> database. However, the connect statement never completes, since I don't
> get the output after that, hence I don't believe that selecting a
> database will help.
>
> Just for grins (and because I'm running out of ideas) I did try it and
> no surprise, it did not complete either.
>
> -MikeB
>
>

you could enable the general query log, (or the error log, or both)
and see if that brings any light in the darkness....

http://dev.mysql.com/doc/refman/5.1/en/query-log.html


-- 
Luuk
0
Reply luuk (814) 9/21/2010 5:57:06 PM

MikeB wrote:
> I installed the WAMP bundle with EasyPHP 5.3.3. Seems to be mostly
> working. At least I can create and display PHP pages in my localhost
> server.
>
> I have defined (just for testing) a user in my SQL named "pubuser" and
> granted it access to a database "publications." Of course I also created
> the database and two tables.
>
> I can access and manipulate the tables via phpMyAdmin and I can log in
> to sql using pubuser via the command-line interface.
>
> I can also telnet to localhost port 3306 and I get something back before
> the port disconnects asfter a short while. THe return string looks like
> this:
>
>  > B
>  > 5.1.49-community-lo6IJyD%*☻L9wl8qR&cA[(
>
> I have the following php code in a file named a login.php.
>
> <?php // login.php
> $db_hostname = 'localhost';
> $db_database = 'publications';
> $db_username = 'pubuser';
> $db_password = 'abc';
> ?>
>
>
> also I have a test web page:
>
> <html>
> <head>
> </head>
> <body>
> <?php
> require_once 'login.php';
> $db_server = mysql_connect($db_hostname, $db_username, $db_password);
> if (!$db_server)
> die("Unable to connect to MySQL: " . mysql_error());
> echo "Congrats, it seems you have connected to the server <br />
> host: $db_hostname<br />
> user: $db_username<br />
> password: $db_password <br />
> for database: $db_database<br />";
> print_r($db_server);
> ?>
> </body>
> </html>
>
> If I try to run this, the broser status message briefly says "connecting
> to localhost" and then indefinitely it says "waiting for localhost..."
>
> The browser window remains blank.
>
> I have disabled my firewall, but the results remain the same.
>
> If I comment out the connect statement, the "die" statement gets
> executed. What should I look for?
>
>
> Thanks
> MikeB


I got it.

Windows etc\hosts file had the mapping of 127.0.0.1 to localhost 
commented out. I fixed that and all is well in sunny-land.  :)
0
Reply MPBrede (98) 9/21/2010 6:14:26 PM

4 Replies
153 Views

(page loaded in 0.069 seconds)

Similiar Articles:













7/13/2012 8:25:01 PM


Reply: