0

I've read many questions similar to this but none of the answers have been relevant to me. I'm trying to access a SQL server using PHP. Both the SQL server and PHP are running on my Windows 10 machine. Here is my PHP code (the username and password are arbitrary):

$connection = mysqli_connect('127.0.0.1', 'hmuuser', 'password');

This causes the following error:

PHP Warning:  mysqli_connect(): (HY000/2002): No connection could be made because the target machine actively refused it.

Here is my connection using MSSMS (which is successful):

Connection to 127.0.0.1 using MSSMS

EDIT:

I was able to solve my problem using this link: http://blog.citrix24.com/configure-sql-express-to-accept-remote-connections/

Using this link I enable SQL Browser, and set a static port to connect to SQL server with. However, I'm presented with a new error:

PHP Warning:  mysqli_connect(): MySQL server has gone away in C:\HMUAPI\Index.php on line 7
PHP Warning:  mysqli_connect(): Error while reading greeting packet. PID=9920 in C:\HMUAPI\Index.php on line 7
PHP Warning:  mysqli_connect(): (HY000/2006): MySQL server has gone away in C:\HMUAPI\Index.php on line 7

My code has been changed to the following to cause this error: $connection = mysqli_connect('127.0.0.1:1434', 'hmuuser', 'password');

mcjcloud
  • 351
  • 2
  • 11
  • try using localhost instead of 127.0.0.1. Remote connections are most likely not allowed on your sql server. 127.0.0.1 will act as a remote connection even though it is local. – Harold Scholtz Feb 14 '17 at 06:56
  • I tried using local host, same error. And MSSMS connects using 127.0.0.1 so I don't think that would be the problem. – mcjcloud Feb 14 '17 at 06:59
  • Please have a look on here if the problem still persist: http://php.net/manual/en/function.mssql-connect.php – Jason Clark Feb 14 '17 at 07:16
  • This is a reference to mysql_connect whereas I'm using the updated version (not deprecated) mysqli_connect – mcjcloud Feb 14 '17 at 07:18
  • please check if Either there is a firewall blocking the connection or the process that is hosting the service is not listening on that port. – Jason Clark Feb 14 '17 at 07:26

2 Answers2

0

Add a database name

$connection = mysqli_connect('127.0.0.1', 'hmuuser', 'password','dbname');

Also here is suggest - Actively refused it" means that the host sent a reset instead of an ack when you tried to connect. It is therefore not a problem in your code. Either there is a firewall blocking the connection or the process that is hosting the service is not listening on that port. This may be because it is not running at all or because it is listening on a different port.

Community
  • 1
  • 1
Alexey Shatrov
  • 450
  • 4
  • 12
0

I guess the problem is, that you use MySQLi to connect to a MSSQL server.

You should use the correct MSSQL extension instead. Microsoft published their SQL-Server for PHP driver on GitHub: https://github.com/Microsoft/msphpsql Or you use the older extension which is documented in the PHP manual: http://de2.php.net/manual/en/book.mssql.php And at least you can set up ODBC.

One of them should resolve your problem.