douyue1998 2017-02-14 06:51
浏览 169

无法使用mysqli_connect连接到SQL Server,但MSSMS连接正常

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');

  • 写回答

2条回答 默认 最新

  • dsjuimtq920056 2017-02-14 06:57
    关注

    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.

    评论

报告相同问题?