Chapter 4: Build Your Own Database, By Kevin Yank

Hi Guys,

I am attempting to use PHP to start outputting data from MySQL and I am running into the error:

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

After searching the web and this forum, the only fix offered was go change “localhost” in line 2 of the below code to 127.0.0.1, which gave me the same error.

Here is my code, copied directly from this website, any ideas?

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

if (!mysqli_set_charset($link, 'utf8'))
{
	$output = 'Unable to set database connection encoding.';
	include 'output.html.php';
	exit();
}

if (!mysqli_select_db($link, 'ijdb'))
{
	$output = 'Unable to locate the joke database.';
	include 'output.html.php';
	exit();
}

$output = 'Database connection established.';
include 'output.html.php';
?>

Thanks!

This error means the mysqli extension is not installed or enabled on your server. No changing of your code will fix the problem that you can’t use something which is not installed.

http://php.net/manual/en/install.windows.extensions.php

Thank you for the reply.

After reading your link I have to one possible solution, but I have another question before I act on it.

It looks as if I need to edit this piece of code within my php.ini file:

; Directory in which the loadable extensions (modules) reside.
; PHP: Description of core php.ini directives - Manual
; extension_dir = “./”
; On windows:
; extension_dir = “C:\PHP\ext”

My question is should i edit the first “extension_dir” or the second “extension_dir.” In addition to this should I have it point to my ext folder within php?

Thank you in advance for your quick response!

You probably shouldn’t change that if your code other than database calls is working.

Is there a line like this (search for ‘mysqli’)?

; extension=php_mysqli_libmysql.dll

If so, remove the semicolon (that tells PHP to ignore that line, effectively disabling that extension) and restart apache.

I do have:

extension=php_mysqli.dll

and per the instructions of the book I already deleted the “;” before it. I have also restarted my computer many times since I made that change.

I do not have anything with an underscore after mysqli in my php.ini folder.

Any other help is much appriciated.

Thanks again.

Is that file php_mysqli.dll in your extension directory?

Assuming my extenstion directory is my ext folder, it does contain php_mysqli.dll.

In that case, you might as well try uncomminting the ext_dir line and setting it to that directory. I don’t know what else to try. I’m just a passer-by… :slight_smile:

Well my symptoms have changed with the update to the php.ini file. I am now getting:

Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user ‘root’@‘localhost’ (using password: YES) in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\connect\index.php on line 2

Unable to connect to the database server.

My codes starts like this:

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

Should I be defining my localhost, root and password within my code?

Thanks for all the help so far, I’m sure you are more than just a “passer-by.” =)

Great, that means you connected to the MySQL server but it rejected your username and password pair. You should change them if “root” and “password” are not what they really are.

Thank you very much Dan for being very easy to understand and answering questions at such a late hour. With your help my problem was solved. The last thing I needed to do was update my password.

I now have the expected result after updating my password in addition to the actions previously taken.

Again, thank you very much and if you see anymore of my questions on this forum, feel free to respond! =)