|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
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 |
|
|
|
|
|
#2 |
|
SitePoint Enthusiast
![]() Join Date: Sep 2009
Location: Rawalpindi, Pakistan
Posts: 47
|
Try to increase timeout properties in my.ini, mysql ini file.
|
|
|
|
|
|
#3 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2009
Posts: 1,415
|
for local server. very funny
I'd check firewall instead |
|
|
|
|
|
#4 |
|
SitePoint Mentor
![]() 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?
|
|
|
|
|
|
#5 |
|
SitePoint Enthusiast
![]() Join Date: Sep 2009
Location: Rawalpindi, Pakistan
Posts: 47
|
Try to unblock 3306 if firewall is turned on as Shrapnel_N5 pointed out.
|
|
|
|
|
|
#6 |
|
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 |
|
|
|
|
|
#7 |
|
SitePoint Enthusiast
![]() 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.
|
|
|
|
|
|
#8 |
|
SitePoint Mentor
![]() Join Date: May 2007
Location: Poole, UK
Posts: 1,253
|
Have you checked that MySQL is running?
|
|
|
|
|
|
#9 |
|
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 |
|
|
|
|
|
#10 |
|
SitePoint Mentor
![]() 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.
|
|
|
|
|
|
#11 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() 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 |
|
|
|
|
|
#12 |
|
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? |
|
|
|
|
|
#13 |
|
SitePoint Mentor
![]() Join Date: May 2007
Location: Poole, UK
Posts: 1,253
|
Run this script in php:
PHP Code:
|
|
|
|
|
|
#14 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() 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 |
|
|
|
|
|
#15 |
|
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
|
|
|
|
|
|
#16 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() 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 |
|
|
|
|
|
#17 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2009
Posts: 1,415
|
is there "Worm protection" thing at Norton?
|
|
|
|
|
|
#18 |
|
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
|
|
|
|
|
|
#19 | |
|
SitePoint Mentor
![]() Join Date: May 2007
Location: Poole, UK
Posts: 1,253
|
Quote:
Getting a connection via mysql instead of mysqli will determine if it is a firewall problem or a problem with the mysqli extenstion. |
|
|
|
|
|
|
#20 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2009
Posts: 1,415
|
no need to install mysql ext
It can be tested using fsockopen() with same result |
|
|
|
|
|
#21 |
|
SitePoint Mentor
![]() 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)?
|
|
|
|
|
|
#22 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2009
Posts: 1,415
|
take a look here http://www.smartftp.com/support/kb/58
|
|
|
|
|
|
#23 | ||
|
SitePoint Mentor
![]() Join Date: May 2007
Location: Poole, UK
Posts: 1,253
|
Quote:
Quote:
|
||
|
|
|
|
|
#24 |
|
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 |
|
|
|
|
|
#25 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() 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:
|
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 22:27.











Linear Mode
