Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 23, 2009, 12:27   #1
oharamj2
SitePoint Member
 
Join Date: Nov 2009
Posts: 7
Problem Connecting to MySQL DB Chapter 4 PHP/MYSQL book

Hi,
I am working my way through Kevin Yanks book "Build your own database driven website using PHP/MySQL" and have reached Chapter 4 where I am having problems connecting to the MySQL database using PHP.
The PHP code I am using is as per the book (in a file called index.php, in a directory called Connect);

<?php
$link = mysqli_connect('localhost', 'root', 'my password');
if (!$link)
{
$output = 'Unable to connect to the database server.';
include 'output.html.php';
exit();
}
$output = 'Database connection established.';
include 'output.html.php';

?>

Output.html.php just echoes the contents of $output

If I run this from a browser (IE) , the browser hangs for a minute then displays the following three paragraphs;
======
Warning: mysqli_connect() [function.mysqli-connect]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306) in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\connect\index.php on line 3

Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\connect\index.php on line 3

Unable to connect to the database server.
=======
I have installed Apache, PHP and MySQL on my Windows Vista PC, and all are running OK. I can access the MySQL server from the command prompt.
Any help appreciated
oharamj2 is offline   Reply With Quote
Old Nov 23, 2009, 12:48   #2
rashedirshad
SitePoint Enthusiast
 
rashedirshad's Avatar
 
Join Date: Sep 2009
Location: Rawalpindi, Pakistan
Posts: 47
Try to increase timeout properties in my.ini, mysql ini file.
rashedirshad is offline   Reply With Quote
Old Nov 23, 2009, 12:51   #3
Shrapnel_N5
SitePoint Wizard
 
Shrapnel_N5's Avatar
 
Join Date: Oct 2009
Posts: 1,415
for local server. very funny
I'd check firewall instead
Shrapnel_N5 is offline   Reply With Quote
Old Nov 23, 2009, 12:58   #4
SpacePhoenix
SitePoint Mentor
 
SpacePhoenix's Avatar
 
Join Date: May 2007
Location: Poole, UK
Posts: 1,253
Is this from the example that starts on page 147 and goes on till page 149?
SpacePhoenix is offline   Reply With Quote
Old Nov 23, 2009, 13:02   #5
rashedirshad
SitePoint Enthusiast
 
rashedirshad's Avatar
 
Join Date: Sep 2009
Location: Rawalpindi, Pakistan
Posts: 47
Try to unblock 3306 if firewall is turned on as Shrapnel_N5 pointed out.
rashedirshad is offline   Reply With Quote
Old Nov 23, 2009, 13:02   #6
oharamj2
SitePoint Member
 
Join Date: Nov 2009
Posts: 7
No this is from page 117 'Connecting to MySQL with PHP'.

Regarding the firewall, I have tried turning my Norton Firewall off completely but I still had the same problem
oharamj2 is offline   Reply With Quote
Old Nov 23, 2009, 13:05   #7
rashedirshad
SitePoint Enthusiast
 
rashedirshad's Avatar
 
Join Date: Sep 2009
Location: Rawalpindi, Pakistan
Posts: 47
Try error_reporting(E_ALL); on the top of the page and then reload the page.
rashedirshad is offline   Reply With Quote
Old Nov 23, 2009, 13:08   #8
SpacePhoenix
SitePoint Mentor
 
SpacePhoenix's Avatar
 
Join Date: May 2007
Location: Poole, UK
Posts: 1,253
Have you checked that MySQL is running?
SpacePhoenix is offline   Reply With Quote
Old Nov 23, 2009, 13:20   #9
oharamj2
SitePoint Member
 
Join Date: Nov 2009
Posts: 7
As suggested I have added error_reporting(E_ALL) to the top of the page and reloaded but got further info than before.

I am able to access the MySQL server from the command prompt
oharamj2 is offline   Reply With Quote
Old Nov 23, 2009, 13:37   #10
SpacePhoenix
SitePoint Mentor
 
SpacePhoenix's Avatar
 
Join Date: May 2007
Location: Poole, UK
Posts: 1,253
Try connecting via the mysql_connect() function instead of the mysqli_connect() function (using the name details) if that connects ok then it is likely a problem with the mysqli on the server.
SpacePhoenix is offline   Reply With Quote
Old Nov 23, 2009, 13:41   #11
Shrapnel_N5
SitePoint Wizard
 
Shrapnel_N5's Avatar
 
Join Date: Oct 2009
Posts: 1,415
there is no such thing, "mysqli on the server"

I'd say it's firewall.
and it is not so easy to turn it off
Shrapnel_N5 is offline   Reply With Quote
Old Nov 23, 2009, 14:08   #12
oharamj2
SitePoint Member
 
Join Date: Nov 2009
Posts: 7
I did try switching from mysqli_connect to mysql_connect and instantly got the following message;

Fatal error: Call to undefined function mysql_connect() in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\connect\index.php on line 3

.....which suggests that the mysqli-connect was OK.

Regarding the firewall, I have checked in the Windows Security Centre and both the Norton and Windows firewalls are turned off. Is there anywhere else I should look?
oharamj2 is offline   Reply With Quote
Old Nov 23, 2009, 14:13   #13
SpacePhoenix
SitePoint Mentor
 
SpacePhoenix's Avatar
 
Join Date: May 2007
Location: Poole, UK
Posts: 1,253
Run this script in php:

PHP Code:

<?php phpinfo(); ?>

Does the result of that mention either mysql or mysqli anywhere?
SpacePhoenix is offline   Reply With Quote
Old Nov 23, 2009, 14:21   #14
Shrapnel_N5
SitePoint Wizard
 
Shrapnel_N5's Avatar
 
Join Date: Oct 2009
Posts: 1,415
yes. look at the norton settings. there are a lot of it
not only firewall, but various threat protection, risk detection and so on

according yo my experience you just cannot get rid of it
And it prevents web-server from connectiong to sql
Shrapnel_N5 is offline   Reply With Quote
Old Nov 23, 2009, 14:25   #15
oharamj2
SitePoint Member
 
Join Date: Nov 2009
Posts: 7
I ran the phpinfo script and it produced a lot of data, including a couple of tables referring to MSQLI
oharamj2 is offline   Reply With Quote
Old Nov 23, 2009, 14:29   #16
Shrapnel_N5
SitePoint Wizard
 
Shrapnel_N5's Avatar
 
Join Date: Oct 2009
Posts: 1,415
yes, you have mysqli support enabled. it is obvious from error message
answering such questions is a waste of time

now he'll ask you to install mysql extension. with same result.
because extension does nothing to connection
Shrapnel_N5 is offline   Reply With Quote
Old Nov 23, 2009, 14:32   #17
Shrapnel_N5
SitePoint Wizard
 
Shrapnel_N5's Avatar
 
Join Date: Oct 2009
Posts: 1,415
is there "Worm protection" thing at Norton?
Shrapnel_N5 is offline   Reply With Quote
Old Nov 23, 2009, 14:54   #18
oharamj2
SitePoint Member
 
Join Date: Nov 2009
Posts: 7
I have gone through all the various Norton settings, disabling where I can (including Intrusion Detection) and still no luck
oharamj2 is offline   Reply With Quote
Old Nov 23, 2009, 14:58   #19
SpacePhoenix
SitePoint Mentor
 
SpacePhoenix's Avatar
 
Join Date: May 2007
Location: Poole, UK
Posts: 1,253
Quote:
Originally Posted by Shrapnel_N5 View Post
yes, you have mysqli support enabled. it is obvious from error message
answering such questions is a waste of time

now he'll ask you to install mysql extension. with same result.
because extension does nothing to connection
There have been people posting problems with scripts from the book, some of them have been down to certain combinations of MySQL and PHP not getting along with each other.

Getting a connection via mysql instead of mysqli will determine if it is a firewall problem or a problem with the mysqli extenstion.
SpacePhoenix is offline   Reply With Quote
Old Nov 23, 2009, 15:04   #20
Shrapnel_N5
SitePoint Wizard
 
Shrapnel_N5's Avatar
 
Join Date: Oct 2009
Posts: 1,415
no need to install mysql ext
It can be tested using fsockopen()
with same result
Shrapnel_N5 is offline   Reply With Quote
Old Nov 23, 2009, 15:09   #21
SpacePhoenix
SitePoint Mentor
 
SpacePhoenix's Avatar
 
Join Date: May 2007
Location: Poole, UK
Posts: 1,253
On a related note, oharamj2 did you install Apache, MySQL and PHP separately or altogether (using a WAMP server)?
SpacePhoenix is offline   Reply With Quote
Old Nov 23, 2009, 15:18   #22
Shrapnel_N5
SitePoint Wizard
 
Shrapnel_N5's Avatar
 
Join Date: Oct 2009
Posts: 1,415
take a look here http://www.smartftp.com/support/kb/58
Shrapnel_N5 is offline   Reply With Quote
Old Nov 23, 2009, 15:22   #23
SpacePhoenix
SitePoint Mentor
 
SpacePhoenix's Avatar
 
Join Date: May 2007
Location: Poole, UK
Posts: 1,253
Quote:
Originally Posted by Shrapnel_N5 View Post
no need to install mysql ext
It can be tested using fsockopen()
with same result
Based on what I've read in the PHP manual, I would not rely on fsockopen() as a 100% foolproof way of testing a connection as there is a warning about it

Quote:
Warning UDP sockets will sometimes appear to have opened without an error, even if the remote host is unreachable. The error will only become apparent when you read or write data to/from the socket. The reason for this is because UDP is a "connectionless" protocol, which means that the operating system does not try to establish a link for the socket until it actually needs to send or receive data.
SpacePhoenix is offline   Reply With Quote
Old Nov 23, 2009, 15:27   #24
oharamj2
SitePoint Member
 
Join Date: Nov 2009
Posts: 7
Problem has now been resolved!

Stumbled across the following on another forum (http://uk.php.net/manual/en/function...-connect.php);
=======
On Windows Vista or above, an entry in the Windows/System32/drivers/etc/hosts file causes mysql_connect() connections to "localhost" to timeout and never connect. This happens on php 5.3 and above since it now uses mysql native driver which has changed it connection behavior compared to libmysql.dll in previous versions. It is not a PHP bug, but definitely a configuration issue for users on new windows systems.

To get around this, you must remove the entry like this:
::1 localhost

and make sure you still have:
127.0.0.1 localhost

Also, you could change the code to connect to the ip instead, but that is inconvenient if you have many web sites.

This issue occurs on Windows Vista, Windows 7 and Windows Server 2008.

=======

I made the changes as suggested and have successfully connected to the database.

Thanks to all for the help/advice/suggestions
oharamj2 is offline   Reply With Quote
Old Nov 23, 2009, 15:30   #25
Shrapnel_N5
SitePoint Wizard
 
Shrapnel_N5's Avatar
 
Join Date: Oct 2009
Posts: 1,415
1. this is TCP socket, not UDP
2. we need it refused to proof, not opened
PHP Code:

<?
$fp
=fsockopen("localhost",3306);
var_dump($fp);
fclose($fp);
if it fails, web-server can't connect to mysql server
Shrapnel_N5 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

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
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 22:27.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved