Link to home
Create AccountLog in
Avatar of detox1978
detox1978Flag for United Kingdom of Great Britain and Northern Ireland

asked on

mysql_connect returns error 500

hi All,

I recently installed MySQL and PHP on a Windows SBS 2011 running IIS7.5

I can connect to MySQL and PHP is running ok.

However when I link the two together using the code below I get error 500

<?php 
$link = mysql_connect('host', 'user', 'password'); 
if (!$link) { 
die('Could not connect: ' . mysql_error()); 
} 
echo 'Connected successfully'; 
mysql_close($link); 
?>

Open in new window



Any suggestions?
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Check to see if PHP is running in the web server.  Can you run phpinfo()?
<?php
phpinfo();
?>

Open in new window

Avatar of detox1978

ASKER

yes PHP is running ok
here's the only reference to MySQL in PHP

User generated image
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Is there a way to check as I'd rather not restart the server
There is no check needed.  If it was enabled, it would have been listed in the phpinfo() data.  I don't know if you will have to restart the server, it depends on your configuration.  But sometimes there isn't any choice because the 'php.ini' is sometimes read only when the server is started.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
With error messages enabled it returns

PHP Fatal error: Call to undefined function mysql_connect() in C:\inetpub\wwwroot\intranet\test.php on line 4


Very interested to read PHP is phasing mysql_connect out.  Looking at the two methods it seems mysqli would make more sense, as it's very similar.

I have PHP Version 5.4.16 installed.  It says on here

PHP 5.3.0+

On Windows, for PHP versions 5.3 and newer, the mysqli extension is enabled and uses the MySQL Native Driver by default. This means you don't need to worry about configuring access to libmysql.dll.

Should mysqli work without configuring?
I get the following message

PHP Fatal error: Class 'mysqli' not found in C:\inetpub\wwwroot\intranet\test.php on line 10
Yes, I think you will be OK using MySQLi.  This article shows how to make the conversion for existing MySQL scripts and provides a mapping of the MySQL to MySQLi programming.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
With the sample below (CONNECT TO THE DATABASE settings change)

<?php


// CONNECT TO THE DATABASE
	$DB_NAME = 'DATABASE_NAME';
	$DB_HOST = 'DATABASE_HOST';
	$DB_USER = 'DATABASE_USER';
	$DB_PASS = 'DATABASE_PASSWORD';
	
	$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
	
	if (mysqli_connect_errno()) {
		printf("Connect failed: %s\n", mysqli_connect_error());
		exit();
	}

// A QUICK QUERY ON A FAKE USER TABLE
	$query = "SELECT * FROM `users` WHERE `status`='bonkers'";
	$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

// GOING THROUGH THE DATA
	if($result->num_rows > 0) {
		while($row = $result->fetch_assoc()) {
			echo stripslashes($row['username']);	
		}
	}
	else {
		echo 'NO RESULTS';	
	}
	
// CLOSE CONNECTION
	mysqli_close($mysqli);
	

?>

Open in new window



I get the following error message

PHP Fatal error: Class 'mysqli' not found in C:\inetpub\wwwroot\intranet\test.php on line 10
Good news!  This is all documented in the online manual!
http://php.net/manual/en/mysqli.installation.php
The documentation says it should already work.  However it doesnt.
the only reference to MySQL in PHP
In my system I see this (sorry about the formatting)
mysql
MySQL Support	enabled
Active Persistent Links 	0
Active Links 	0
Client API version 	5.1.70
MYSQL_MODULE_TYPE 	external
MYSQL_SOCKET 	/var/lib/mysql/mysql.sock
MYSQL_INCLUDE 	-I/usr/include/mysql
MYSQL_LIBS 	-L/usr/lib64 -lmysqlclient

Directive	Local Value	Master Value
mysql.allow_local_infile	On	On
mysql.allow_persistent	On	On
mysql.connect_timeout	60	60
mysql.default_host	no value	no value
mysql.default_password	no value	no value
mysql.default_port	no value	no value
mysql.default_socket	/var/lib/mysql/mysql.sock	/var/lib/mysql/mysql.sock
mysql.default_user	no value	no value
mysql.max_links	Unlimited	Unlimited
mysql.max_persistent	Unlimited	Unlimited
mysql.trace_mode	Off	Off


mysqli
MysqlI Support	enabled
Client API library version 	5.1.70
Active Persistent Links 	0
Inactive Persistent Links 	0
Active Links 	0
Client API header version 	5.1.70
MYSQLI_SOCKET 	/var/lib/mysql/mysql.sock

Directive	Local Value	Master Value
mysqli.allow_local_infile	On	On
mysqli.allow_persistent	On	On
mysqli.default_host	no value	no value
mysqli.default_port	3306	3306
mysqli.default_pw	no value	no value
mysqli.default_socket	/var/lib/mysql/mysql.sock	/var/lib/mysql/mysql.sock
mysqli.default_user	no value	no value
mysqli.max_links	Unlimited	Unlimited
mysqli.max_persistent	Unlimited	Unlimited
mysqli.reconnect	Off	Off


PDO
PDO support	enabled
PDO drivers 	sqlite, sqlite2, mysql

Open in new window

This is a Windows server.  Any idea how i can see the same info on Windows?
Got there in the end.  I added extension_dir=c:\php\ext


many thanks
Check your 'php.ini' and make sure that the 'extension=php_mysqli.dll' does not have a ';' in front of it.  You may have to restart your server for it to take effect if you have to change it.
I just downloaded a fresh copy of PHP 5.4.17 from http://windows.php.net/download/ .  It does not have any of the MySQL extensions enabled.  I've installed PHP on Windows so many times that I automatically go edit 'php.ini' to set up the extensions that I want.  It comes with almost everything turned off.