Build your own Database Driven Webiste, Chapter 4

I’m in Chapter 4 (page 121, 4th edition). I am trying to connect to my database for the first time through my webbrowser/php.

I receive the following error when I try to connect my page with my jokes database:

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

I have tried manually creating the code and also copying the provided code from the text book. Any advice at all regarding this matter would be great!

Thank you

I had the same problem in Windows 7. The error message began “Warning: mysql_connect() [function.mysql-connect]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306)”

I only found the solution by reading various snippets of advice in different places. Here is the way to fix this problem, all in one place:

First, close all open programs. I stopped the Apache service and the mysql service using Windows Task Manager (click on the services tab and then on the services button in the lower right corner. Find the services in the list and select and stop each in turn.)

Next, run Notepad as administrator. To do that, find Notepad in the Windows Start menu, right click the icon and select Run as Administrator. Now, navigate to C:\Windows\System32\drivers\etc and find and open the hosts file.

Find the line that looks like this:

127.0.0.1 localhost

Uncomment that line by removing the # from the line and then save the hosts file. Close Notepad and then restart Windows.

This worked for me. Good luck. Thanks to Patrício dos Santos in the mysql forums for most of this.

jburns57

You might verify you have the mysqli library compiled into php. Another option would be to use mysql_connect instead of mysqli.

Good Luck

Since that function is part of PHP5, could it be possible that you have php4 and 5 installed and Apache is running your script with PHP4?

Have you taken a look at your phpinfo output? Create a php file with

<?php phpinfo(); ?>

and take a look at the output. Or even post it here.

Hi guys I’m having the same problem as Alex unfortunately I am a real beginner so not sure what your talking about in your replies I am assuming that for the mysqli_connect you need to change the code on the index.php file within the connect folder tried that and it didn’t work I changed the password to the password i created in mysqladmin still getting error message any ideas please would be gratefully received.

Do that then:

  1. Make a note of the value given for “Loaded Configuration File”
  2. Open that file up in Notepad.
  3. Scrolls down the file until you see a line with “extension=php_mysqli.dll”
  4. If that line has a semi-colon at the start of it, remove the ;
  5. Save the file don’t use “Save As” use “Save”
  6. Restart the server.

Hi Space Phoenix, thanks for the quick response I’m not sure how I’m supposed to run that php script I assumed you put it into an html page and got nothing back, when I check my apache server is says its running and it shows on the bottom Apache/2.2.13(win32) PHP/5.3.0 realise that perhaps someone so thick shouldn’t be trying to write database driven websites but I love a challenge. once again thanks for the help any ideas on where to go from here (please don’t suggest Siberia! I hate the cold.)

PHP scripts need to be in files with the extension .php

Hi SpacePhoenix
thanks again for the prompt reply didn’t spot my obvious mistake changed the page to php and I get a page that shows PHP Version 5.3.0 with a big list of what is enabled etc then below that configuration with apache2handler followed by lots of other bits and pieces. once again thanks for the reply

I’m having the same issue as the other challenged folks here. I have followed the suggestion from SpacePhoenix and there was no change needed (that part was configured correctly during setup).

I’m on a Windows Vista 64 bit machine and I have setup up MySql, Apache, and Windows according to the book referenced.

I’ve output phpinfo() and everything is as current as possible.

Is there something else to look at or is there a particular syntax for the following:
$link = mysqli_connect(‘localhost’, ‘root’, ‘password’);

The above leads me to believe that all I need to do is enter my password.

Am I missing something?

Thanks for your help in advance.

Fortunately I am running a mac as well as windows machine and managed to get MAMP up and running which made the whole process a lot simpler, I found a good PHP text editor which is free for windows and mac called komodo it highlights the code for you and if you get wamp up and running you can use it to follow the course. Hope this.

I’m having the same issue here

my php version is current. extension=php_mysqli.dll is not commented in php.ini.

but I am still getting the

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

with the code when I paste it from the book.

was this ever solved? would love to move on.

Is it uncommented in the correct php.ini file? The correct one can be identifed in php info as “Loaded Configuration File”

yep it is, I just did a reboot and now I have a different error:

“Warning: mysqli_connect() [function.mysqli-connect]: (/838860800): in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\connect\index.php on line 2
Unable to connect to the database server.”

which I am guessing just means my password is wrong, had trouble with that cmd in the book too… hmm

final edit: yep, ‘Database connection established.’

reboot and a change of the pw in my code did it. thanks! :slight_smile:

Hi all,

I had the same issue as above, Fatal error: Call to undefined function mysqli_connect() in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\connect\index.php on line 2.

I updated PHP as suggested and it seemed to solve the problem one time. I now get a 500 error on the web page. It doesn’t happen instantly it seems to take a minute or so. I know that the localhost, user name, and password are correct.

Error: The website cannot display the page
HTTP 500
Most likely causes:
•The website is under maintenance.
•The website has a programming error.

What you can try:
Refresh the page.

 Go back to the previous page. 

 More information 

I am on a Windows 7 box, using Apache 2.2, PHP Version 5.3.2, MySQL 5.1.
I used php-5.3.2-Win32-VC6-x86 for Apache.

Any ideas? Anything more I can provide?

Hi gtopple, welcome to the forums,

Did you install the Thread Safe version of PHP? http://www.sitepoint.com/books/phpmysql4/errata.php

I did install the Thread Safe version of PHP

I just got:
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 (x86)\Apache Software Foundation\Apache2.2\htdocs\connect\index.php on line 2

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 (x86)\Apache Software Foundation\Apache2.2\htdocs\connect\index.php on line 2

Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\connect\index.php on line 2

Mittineague, I found the answer on a database thread. Thanks for trying to help.

The solution was to change ‘localhost’ to ‘127.0.0.1’

I am sure I will be on the boards a lot now!

Glad you got things so you can work with them.

I had to use 127.0.0.1 before too. Then I changed something in a configuration file somewhere (sorry, I don’t remember what I did now) so that “localhost” would “map” to the 127.0.0.1 IP

Aaarrrggghhh!!! Installation is NOT my favorite thing.

Mittineague, I think the configuration file your refering to is the HOSTS file.