This code makes nothing happen?

Ok, i have apache running, mysql running, php installed all ok on my desktop pc. I know this because i can see my localhost and i can run phpinfo.php and get the full details.

I am just trying to make a DB connection. I am running this code;

<?php
$mysqli = mysqli_connect("localhost", "correctuser", "correctpassword", "correctdbname");

if (mysqli_connect_errno()) {
	printf("Connect failed: %s\
", mysqli_connect_error());
	exit();
} else {
	printf("Host information: %s\
", mysqli_get_host_info($mysqli));
}
?>

when i run this file in my browser i get a blank screen. when i view source there is nothing…?

This code is exactly copied from the book I am following, and even the same as the downloadable code that comes with the book.

I’m at a loss. Even the first steps to learning all this stuff is tripping me up :frowning:

…and with this?


<?php
error_reporting(-1);
ini_set('display_errors', true);

$conn = mysqli_connect('host', 'user', 'pass', 'database', 3306);

if(false === is_resource($conn)){
    printf(
        'Cannot connect: &#37;s',
        mysqli_connect_error()
    );
    exit;
}

printf(
    'Connection Information: %s',
    mysqli_get_server_info()
);
?>

Thanks, I get this;

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

It appears the mysqli extension is not enabled, check to see of it’s listed in phpinfo(). If not, enable it by uncommenting it in php.ini and ensuring the php_mysql.dll is properly installed.

:slight_smile:

Thanks for your help so far AnthonySterling !

I uncommented the line in my php.ini

extension=php_mysqli.dll

and i do seem to have a file called php_mysql.dll in my c:/php/ext folder, but am unsure if that means it is installed fully/correctly.

I have rebooted machine, localhost is working ok, but I am still getting the error message

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

Whoops, slight typo on my part. The file you’re looking for is php_mysqli.dll, is that file in your extensions directory and enabled in your ini?

Yes and yes :frowning: but still getting that error

looking at my phpinfo i noticed;

Configuration File (php.ini) Path C:\WINDOWS
Loaded Configuration File C:\php\php.ini

does that seem right?

and in php variables;

_SERVER[“PATH”] C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\MySQL\MySQL Server 5.0\bin

which is weird as my PATH is actually

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\MySQL\MySQL Server 5.0\bin;C:\php

so the phpinfo is missing the “C:\php” at the end?!? very odd, have rebooted and rechecked but cant explain this…?

Try this from the manual. :slight_smile:

Installation on Windows Systems

On Windows, PHP is most commonly installed using the binary installer. Once PHP has been installed, some configuration is required to enable mysqli and specify the client library you want it to use.

As mentioned earlier, the mysqli extension is not enabled by default, so the php_mysqli.dll DLL must be enabled inside of php.ini. In order to do this you need to find the php.ini file (typically located in c:\php), and make sure you remove the comment (semi-colon) from the start of the line extension=php_mysqli.dll, in the section marked [PHP_MYSQLI].

Also, if you want to use the MySQL Client Library with mysqli, you need to make sure PHP can access the client library file. The MySQL Client Library is included as a file named libmysql.dll in the Windows PHP distribution. This file needs to be available in the Windows system’s PATH environment variable, so that it can be successfully loaded. See the FAQ titled “How do I add my PHP directory to the PATH on Windows” for information on how to do this. Copying libmysql.dll to the Windows system directory (typically c:\Windows\system) also works, as the system directory is by default in the system’s PATH. However, this practice is strongly discouraged.

On Windows, for PHP versions 5.3 and newer, the mysqli extension uses the MySQL Native Driver by default. This means you don’t need to worry about configuring access to libmysql.dll, you just need to make sure the extension is enabled in the php.ini file.

As with enabling any PHP extension (such as php_mysqli.dll), the PHP directive extension_dir should be set to the directory where the PHP extensions are located. See also the Manual Windows Installation Instructions. An example extension_dir value for PHP 5 is c:\php\ext.

Hmm, fixed that last problem with the PATH file by doing this

Click on ‘New’ and enter ‘PHPRC’ as the variable name and the directory where php.ini is located as the variable value (e.g. C:\php)

Still getting that same error message though! :frowning:

Have you restarted Apache and checked phpinfo() ?

Thanks AnthonySterling, I really appreciate the advice and moral support!

My ext dir was not set in my php.ini

I have now progressed to a new error, something about my failed password while attempting to connect, I’ll start afresh in the morning and get to work on that :slight_smile:

Ah, excellent news. I guess. :stuck_out_tongue:

…and you’re most welcome.