FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Not bale to connect to MySQL with PHP
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Not bale to connect to MySQL with PHP [message #169640] Mon, 20 September 2010 19:32 Go to next message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
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 have the following php code:

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


also I have a test web page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Connect to SQL</title>
</head>
<body>
<?php
Echo "Hello";
require_once 'login.php';
echo "got require";
$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, it briefly says "connecting to localhost" and then
indefinitely it says "waiting for localhost..."

If I comment out the connect statement, the rest runs. What should I
look for?

Thanks
MikeB
Re: Not bale to connect to MySQL with PHP [message #169641 is a reply to message #169640] Mon, 20 September 2010 20:04 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 20/09/10 20:32, MikeB wrote:
> 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 have the following php code:
>
> <?php // login.php
> $db_hostname = 'localhost';
> $db_database = 'publications';
> $db_username = 'pubuser';
> $db_password = 'abc';
> ?>
>
>
> also I have a test web page:
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> <html>
> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
> <title>Connect to SQL</title>
> </head>
> <body>
> <?php
> Echo "Hello";
> require_once 'login.php';
> echo "got require";
> $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, it briefly says "connecting to localhost" and then
> indefinitely it says "waiting for localhost..."
>
> If I comment out the connect statement, the rest runs. What should I
> look for?

Are you doing all of this on a single machine? Or do have a separate
server and user machine?

localhost to the php process means the web server - so it's looking for
mysql on the web server, which is not necessarily the machine you're
running the browser on.

Are mysql and php in agreement as to which port to use?

What hosts(s) are defined in phpmyadmin for the user?

can you telnet to the mysql port on the server from a terminal session
on the server? eg telnet to the server, and then on the server "telnet
localhost 3306" (or whatever your mysql port is). What happens?

Rgds

Denis McMahon
Re: Not bale to connect to MySQL with PHP [message #169642 is a reply to message #169641] Mon, 20 September 2010 22:05 Go to previous messageGo to next message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
Denis McMahon wrote:
> On 20/09/10 20:32, MikeB wrote:
>> 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 have the following php code:
>>
>> <?php // login.php
>> $db_hostname = 'localhost';
>> $db_database = 'publications';
>> $db_username = 'pubuser';
>> $db_password = 'abc';
>> ?>
>>
>>
>> also I have a test web page:
>>
>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
>> <html>
>> <head>
>> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
>> <title>Connect to SQL</title>
>> </head>
>> <body>
>> <?php
>> Echo "Hello";
>> require_once 'login.php';
>> echo "got require";
>> $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, it briefly says "connecting to localhost" and then
>> indefinitely it says "waiting for localhost..."
>>
>> If I comment out the connect statement, the rest runs. What should I
>> look for?
>
> Are you doing all of this on a single machine? Or do have a separate
> server and user machine?

I'm learning PHP and MySQL from an O"Reilly book. The book had me
download and install EasyPHP. So yes, everything is on my machine. Up to
now all my PHP-only examples have worked just fine.
>
> localhost to the php process means the web server - so it's looking for
> mysql on the web server, which is not necessarily the machine you're
> running the browser on.

I think in this instance it is.

>
> Are mysql and php in agreement as to which port to use?

In phpinfo(), it says:

mysql.default_port no value no value
mysql.default_socket no value no value

The mysqli section says:
mysqli.default_host no value no value
mysqli.default_port 3306 3306

The [client] section of my.ini has

port = 3306
socket = /tmp/mysql.sock

and
[mysqld]
port=3306
bind-address = 127.0.0.1
socket = /tmp/mysql.sock
>
> What hosts(s) are defined in phpmyadmin for the user?

localhost or 127.0.0.1

>
> can you telnet to the mysql port on the server from a terminal session
> on the server? eg telnet to the server, and then on the server "telnet
> localhost 3306" (or whatever your mysql port is). What happens?

I think this is just for the case where the two are on different
servers, right?

Thanks for your help on this.
Re: Not bale to connect to MySQL with PHP [message #169645 is a reply to message #169642] Tue, 21 September 2010 12:49 Go to previous messageGo to next message
Kim Andr Aker is currently offline  Kim Andr Aker
Messages: 17
Registered: September 2010
Karma: 0
Junior Member
På Tue, 21 Sep 2010 00:05:54 +0200, skrev MikeB <mpbrede(at)gmail(dot)com>:

> Denis McMahon wrote:
>> On 20/09/10 20:32, MikeB wrote:
>>> [snip]
>>
>> can you telnet to the mysql port on the server from a terminal session
>> on the server? eg telnet to the server, and then on the server "telnet
>> localhost 3306" (or whatever your mysql port is). What happens?
>
> I think this is just for the case where the two are on different
> servers, right?

Only the first step of the two. Since this is on your local computer, just
open up a command window and enter the command "telnet localhost 3306" -
and to repeat Denis' question: What happens when you do this?

If you still get an error (as in "connection refused"), then the MySQL
server software probably isn't running.


--
Kim André Akerø
- kimandre(at)NOSPAMbetadome(dot)com
(remove NOSPAM to contact me directly)
Re: Not bale to connect to MySQL with PHP [message #169646 is a reply to message #169645] Tue, 21 September 2010 13:55 Go to previous messageGo to next message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
On Tue, 21 Sep 2010 14:49:47 +0200, Kim André Akerø wrote:
> På Tue, 21 Sep 2010 00:05:54 +0200, skrev MikeB <mpbrede(at)gmail(dot)com>:
>
>> Denis McMahon wrote:
>>> On 20/09/10 20:32, MikeB wrote:
>>>> [snip]
>>>
>>> can you telnet to the mysql port on the server from a terminal session
>>> on the server? eg telnet to the server, and then on the server "telnet
>>> localhost 3306" (or whatever your mysql port is). What happens?
>>
>> I think this is just for the case where the two are on different
>> servers, right?
>
> Only the first step of the two. Since this is on your local computer, just
> open up a command window and enter the command "telnet localhost 3306" -
> and to repeat Denis' question: What happens when you do this?
>
> If you still get an error (as in "connection refused"), then the MySQL
> server software probably isn't running.

No.

"localhost" to MySQL is not a network socket that you can telnet to. It
is an OS socket (or equivelent). "Localhost" would be a network socket,
but that's *not* what the OP asked about.

Which is why this may belong on a MySQL group, or may belong on a group
for whatever webserver is running, but the odds of it being an actual
PHP problem are very, very slim.

--
15. I will never employ any device with a digital countdown. If I find
that such a device is absolutely unavoidable, I will set it to
activate when the counter reaches 117 and the hero is just putting
his plan into action. --Peter Anspach's Evil Overlord list
Re: Not bale to connect to MySQL with PHP [message #169649 is a reply to message #169645] Tue, 21 September 2010 14:47 Go to previous messageGo to next message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
Kim André Akerø wrote:
> På Tue, 21 Sep 2010 00:05:54 +0200, skrev MikeB <mpbrede(at)gmail(dot)com>:
>
>> Denis McMahon wrote:
>>> On 20/09/10 20:32, MikeB wrote:
>>>> [snip]
>>>
>>> can you telnet to the mysql port on the server from a terminal session
>>> on the server? eg telnet to the server, and then on the server "telnet
>>> localhost 3306" (or whatever your mysql port is). What happens?
>>
>> I think this is just for the case where the two are on different
>> servers, right?
>
> Only the first step of the two. Since this is on your local computer,
> just open up a command window and enter the command "telnet localhost
> 3306" - and to repeat Denis' question: What happens when you do this?
>
> If you still get an error (as in "connection refused"), then the MySQL
> server software probably isn't running.
>
>

I tried it and it seems to connect to something. I get this:

telnet localhost 3306 and then:

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

and a few seconds later I get:

> Connection to host lost.
>
> C:\Users\Mike>
Re: Not bale to connect to MySQL with PHP [message #169650 is a reply to message #169649] Tue, 21 September 2010 15:08 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 21/09/10 15:47, MikeB wrote:

> I tried it and it seems to connect to something. I get this:
>
> telnet localhost 3306 and then:
>
>> B
>> 5.1.49-community-lo6IJyD%*☻L9wl8qR&cA[(
>
> and a few seconds later I get:
>
>> Connection to host lost.
>>
>> C:\Users\Mike>

OK, that's almost what happens on my working system, except that I don't
see any text from the mysql connection.

However, I overlooked this:

> <?php
> Echo "Hello";
> require_once 'login.php';
> echo "got require";

You didn't say whether any of these echoes were sent to the browser?

Also, as you're in the document body, it's a good idea to wrap those
echoes in suitable html element tags, eg:

<?php
Echo "<p>Hello</p>";
require_once 'login.php';
echo "<p>got require</p>";

and see what happens if you comment out the database stuff:

/*
$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);
*/

?>

Just to check the underlying php / html functionality.

You could also add the following after 'echo "<p>got require</p>";'

echo "<p>db info: user = '$db_username', host = '$db_hostname', pw =
'$db_password'</p>";

Rgds

Denis McMahon
Re: Not bale to connect to MySQL with PHP [message #169651 is a reply to message #169650] Tue, 21 September 2010 16:40 Go to previous messageGo to next message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
Denis McMahon wrote:
> On 21/09/10 15:47, MikeB wrote:
>
>> I tried it and it seems to connect to something. I get this:
>>
>> telnet localhost 3306 and then:
>>
>>> B
>>> 5.1.49-community-lo6IJyD%*☻L9wl8qR&cA[(
>>
>> and a few seconds later I get:
>>
>>> Connection to host lost.
>>>
>>> C:\Users\Mike>
>
> OK, that's almost what happens on my working system, except that I don't
> see any text from the mysql connection.
>
> However, I overlooked this:
>
>> <?php
>> Echo "Hello";
>> require_once 'login.php';
>> echo "got require";
>
> You didn't say whether any of these echoes were sent to the browser?


The echos only get sent to the browser when I //-comment out the
mysql_connect statement.

>
> Also, as you're in the document body, it's a good idea to wrap those
> echoes in suitable html element tags, eg:
>
> <?php
> Echo "<p>Hello</p>";
> require_once 'login.php';
> echo "<p>got require</p>";
>
> and see what happens if you comment out the database stuff:

See above, 'cept I just let the debugging stuff run on to one line.

>
> /*
> $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);
> */
>
> ?>
>
> Just to check the underlying php / html functionality.
>
> You could also add the following after 'echo "<p>got require</p>";'
>
> echo "<p>db info: user = '$db_username', host = '$db_hostname', pw =
> '$db_password'</p>";

Did this also. I get the values in the login.php file. But again, only
if I comment out the mysql_connect statement. If I don't, it just hangs
the entire browser window indefinitely. I just copied the "success" echo
block of the code to before the mysqlL-connect statement.

I'm just plumb out of ideas.

I'm gonna try install a different WAMP package on another computer and
see if I get any further. :(
Re: Not bale to connect to MySQL with PHP [message #169652 is a reply to message #169651] Tue, 21 September 2010 18:14 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 9/21/2010 12:40 PM, MikeB wrote:
> Denis McMahon wrote:
>> On 21/09/10 15:47, MikeB wrote:
>>
>>> I tried it and it seems to connect to something. I get this:
>>>
>>> telnet localhost 3306 and then:
>>>
>>>> B
>>>> 5.1.49-community-lo6IJyD%*☻L9wl8qR&cA[(
>>>
>>> and a few seconds later I get:
>>>
>>>> Connection to host lost.
>>>>
>>>> C:\Users\Mike>
>>
>> OK, that's almost what happens on my working system, except that I don't
>> see any text from the mysql connection.
>>
>> However, I overlooked this:
>>
>>> <?php
>>> Echo "Hello";
>>> require_once 'login.php';
>>> echo "got require";
>>
>> You didn't say whether any of these echoes were sent to the browser?
>
>
> The echos only get sent to the browser when I //-comment out the
> mysql_connect statement.
>
>>
>> Also, as you're in the document body, it's a good idea to wrap those
>> echoes in suitable html element tags, eg:
>>
>> <?php
>> Echo "<p>Hello</p>";
>> require_once 'login.php';
>> echo "<p>got require</p>";
>>
>> and see what happens if you comment out the database stuff:
>
> See above, 'cept I just let the debugging stuff run on to one line.
>
>>
>> /*
>> $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);
>> */
>>
>> ?>
>>
>> Just to check the underlying php / html functionality.
>>
>> You could also add the following after 'echo "<p>got require</p>";'
>>
>> echo "<p>db info: user = '$db_username', host = '$db_hostname', pw =
>> '$db_password'</p>";
>
> Did this also. I get the values in the login.php file. But again, only
> if I comment out the mysql_connect statement. If I don't, it just hangs
> the entire browser window indefinitely. I just copied the "success" echo
> block of the code to before the mysqlL-connect statement.
>
> I'm just plumb out of ideas.
>
> I'm gonna try install a different WAMP package on another computer and
> see if I get any further. :(
>

What happens if you use '127.0.0.1' instead of 'localhost'?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Not bale to connect to MySQL with PHP [message #169653 is a reply to message #169652] Tue, 21 September 2010 18:18 Go to previous messageGo to next message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
Jerry Stuckle wrote:

>>
>
> What happens if you use '127.0.0.1' instead of 'localhost'?
>


Great minds think alike! :)

Some searching led me to this and I found that that worked.

That led me to Windows etc/hosts and I found the mapping of 127.0.0.1 to
localhost was commented out.

While many things worked, this particular interface didn't.

I removed the comment and all is well. localhost as well as 127.0.0.1 is
now fine for connecting to MySQL.

I must say that the way I found that was to get xdebug to work, then I
got an warning message 2002 on the connect statement. That in Google
finally led me to the solution.

I guess I could have waited for you to post as well :) Would have been
less work, sweat and swearwords for me.. :)
Re: Not bale to connect to MySQL with PHP [message #169654 is a reply to message #169651] Tue, 21 September 2010 19:48 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 21/09/10 17:40, MikeB wrote:
> Denis McMahon wrote:

>> You could also add the following after 'echo "<p>got require</p>";'
>>
>> echo "<p>db info: user = '$db_username', host = '$db_hostname', pw =
>> '$db_password'</p>";
>
> Did this also. I get the values in the login.php file. But again, only
> if I comment out the mysql_connect statement. If I don't, it just hangs
> the entire browser window indefinitely. I just copied the "success" echo
> block of the code to before the mysqlL-connect statement.
>
> I'm just plumb out of ideas.
>
> I'm gonna try install a different WAMP package on another computer and
> see if I get any further. :(

Yeah, I think at this point so am I, especially as my experience is a
lamp environment and your windows one seems to be handling itself
slightly differently to what I'd expect.

Rgds

Denis McMahon
Re: Not bale to connect to MySQL with PHP [message #169655 is a reply to message #169653] Tue, 21 September 2010 21:46 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 21/09/10 19:18, MikeB wrote:

> That led me to Windows etc/hosts and I found the mapping of 127.0.0.1 to
> localhost was commented out.

Hmm, some malware has been known to hamper with the hosts file.

I'm pretty sure that the localhost definition isn't normally commented
out on the default file that comes with windows.

Might be a symptom of a bigger underlying problem.

Rgds

Denis McMahon
Re: Not bale to connect to MySQL with PHP [message #169656 is a reply to message #169655] Tue, 21 September 2010 22:26 Go to previous messageGo to next message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
Denis McMahon wrote:
> On 21/09/10 19:18, MikeB wrote:
>
>> That led me to Windows etc/hosts and I found the mapping of 127.0.0.1 to
>> localhost was commented out.
>
> Hmm, some malware has been known to hamper with the hosts file.
>
> I'm pretty sure that the localhost definition isn't normally commented
> out on the default file that comes with windows.
>
> Might be a symptom of a bigger underlying problem.
>

Actually, no. It is documented in Microsoft Technet that this has been
done in Windows releases from Vista on. Unfortunately I don't have a
reference handy right now, but I read up on it this morning.

Seems Microsoft expects DNS service to resolve localhost and that they
are moving away from the hosts file.
Re: Not bale to connect to MySQL with PHP [message #169659 is a reply to message #169656] Tue, 21 September 2010 23:21 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 21/09/10 23:26, MikeB wrote:
> Denis McMahon wrote:
>> On 21/09/10 19:18, MikeB wrote:
>>
>>> That led me to Windows etc/hosts and I found the mapping of 127.0.0.1 to
>>> localhost was commented out.
>>
>> Hmm, some malware has been known to hamper with the hosts file.
>>
>> I'm pretty sure that the localhost definition isn't normally commented
>> out on the default file that comes with windows.
>>
>> Might be a symptom of a bigger underlying problem.
>>
>
> Actually, no. It is documented in Microsoft Technet that this has been
> done in Windows releases from Vista on. Unfortunately I don't have a
> reference handy right now, but I read up on it this morning.
>
> Seems Microsoft expects DNS service to resolve localhost and that they
> are moving away from the hosts file.

Well, I'd suggest that quite obviously the ms dns resolver isn't
resolving localhost without the help of the hosts file.

Where in the dns resolution are they expecting this to take place? In
the local host, or in some upstream dns server?

It seems a bit dim to have to ask my router / isp to resolve localhost
for me if I can do it locally, and if I take that out of the hosts file,
then it still seems that I should have some mechanism on the local
machine to do it instead of the hosts file, which should thus be
enabled, which presumably needs some configuration somewhere - do we add
a registry key?

Rgds

Denis McMahon
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Php Include bug in firefox
Next Topic: Feed RSS and accented and other special chars
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 01:49:25 GMT 2024

Total time taken to generate the page: 0.02728 seconds