Can't connect to MSSQL from PHP - php

I have been trying to connect to my SQL server from my web host using the built-in PHP functions, but no luck so far.
This is my code:
mssql_connect("201.252.95.27:1433/SQLEXPRESS", $sqlServerUsername, $sqlServerpassword);
And this is the error:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server
Tried using "201.252.95.27", "201.252.95.27:1433", "201.252.95.27,1433", "201.252.95.27:1433/SQLEXPRESS" and all possible combinations, but it isn't working.
The server is running and the port is open.

SQL Server usually uses a comma to separate the port, and a backslash to separate the instance name.
Using 201.252.95.27,1433\SQLEXPRESS I was able to connect to your server, but lacking a username/password, I couldnt log in.

Related

Unable to connect to SQL server using mysqli_connect, but MSSMS connects fine

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

Can't connect to remote SQL database in PHP, but remote access is set up and confirmed working

I'm trying to learn to use SQL stuff in PHP but I'm having an issue simply connecting to my database.
The database is almost certainly working with remote access to some extent. I disabled iptables temporarily (I know, bad bad bad!), so there's no firewall blocking access. The MySQL user I am using is configured to be able to connect from any IP. The SQL server is listening properly to all connections and is run on a Linux dedicated server.
[root#1742CC-XEON ~]# netstat -lpnut | grep mysql
tcp 0 0 0.0.0.0:3315 0.0.0.0:* LISTEN 3915/mysqld
I've also used the exact same credentials to successfully remotely connect to my database in Java, like so:
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://__ip__:_port_/_dbname_", "username", "password");
System.out.println("Connection established.");
This functions just fine, and I can successfully run queries, etc. Since I have iptables disabled at the moment this connection functions just fine anywhere that has Java and the Java SQL connector.
Now, I'm trying to connect to the same database in PHP:
$db = mysqli_connect($sql_host, $sql_user, $sql_pass, $sql_db, $sql_port);
And this connection fails with the following error:
Warning: mysqli_connect(): (HY000/2003): Can't connect to MySQL server on 'xx.xx.xx.ip' (111)
Note that $sql_user, $sql_pass, $sql_host, etc. are all the same as the credentials used in the Java connection, so there shouldn't be any issue with the user not being able to access the server from a new IP, since that user is granted access on % and I've made sure that it is indeed able to access the SQL server from multiple IPs not explicitly allowed in the users table. I've also tried different users, including root, and all give the same connection error.
I've spent a few hours looking around but I honestly can't find anything that seems to be the right answer, would really appreciate any help!
Is it possible that this is caused by my webhost? I've tried this script on HostGator webhost as well as the whois.com hosting and both give the same error. Could they possibly be preventing the PHP script from connecting to the remote database?
Have you tried telneting to the server on 3315 from your webhosts?

localhost, Microsoft SQL and php

I just started learning PHP and MySQL, and I got stuck when trying to connect to my database from a PHP block. I have installed XAMPP and SQL Server Express 2012 on my machine. My server name is RAFAL-MAC and I selected 'Windows Authentication.'
I have a simple HTML page with PHP code in a file called search.php
the PHP code is
<?php
$con = mysql_connect('RAFAL-MAC','RAFAL-MAC\Rafal','');
if (!$con)
{
die( 'Could not connect: ' . mysql_error() ) ;
}
mysql_select_db("one", $con) or die(mysql_error());
?>
When I go to localhost:8080/search.php I get the following warning:
Warning: mysql_connect(): Host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server in C:\xampp\htdocs\search_submit.php on line 11
Could not connect: Host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server
I tried searching similar problems online, but all of the threads are about remote connections. I am just trying to connect to my local SQL server.
Any hints? I apologize for any missing information. Thanks! :)
EDIT:
I changed the server name to 'localhost'
Now I am getting this:
Access denied for user ''#'localhost' to database 'one'
Since you'd like to connect to SQL Server Express 2012, all functions beginning with mysql* will fail - since these functions speak to MySQL servers.
Please install Microsoft's MS SQL Server driver pack found here. From this page:
Perform the following steps to download and install the Microsoft
Drivers for PHP for SQL Server:
Download SQLSRV30.EXE to a temporary directory
Run SQLSRV30.EXE
When prompted, enter the path to the PHP extensions directory
After extracting the files, read the Installation section of the SQLSRV30_Readme.htm file for next steps
People in Microsoft's PHP driver forum should easily be able to answer your question, in case you still have problems after the installation of the driver.
Additionally, if connecting to the local machine, try to use these addresses:
localhost
127.0.0.1
These two addresses always reference the local machine.

Cannot connect to SQL SERVER 2000

I am using php 5.3.1 to connect to my SQL SERVER 2000 on remote machine. I use Windows XP.
On using simple program like this:
$conn = mssql_connect("VBNET","sa","mypass") or die ( 'Can not connect to server' );
I get following error:
Warning: mssql_connect() [function.mssql-connect]: message: Login failed for user 'sa'. Reason: Not associated with a trusted SQL Server connection. (severity 14) in C:\wamp\www\Mssql\test.php on line 8
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: VBNET in C:\wamp\www\Mssql\test.php on line 8
Can not connect to server
I enabled TCP/IP and named pipes in my SQL SERVER and restarted it. Also its Authentication is set to Mixed Mode Authentication.
I can see my server through MS SQL Server Management Studio. So what am I missing here ?
Thanks in advance.
P.S. I have another server in SQL SERVER 2008 to which I can easily connect with the same above mentioned connection string.
EDIT: According to this post, I fired sqlcmd -S np:\\VBNET\pipe\sql\query -U sa in cmd and entered the password and that worked !!! So now what could be the problem. I am at my wits end...please help !!
If you can connect using SSMS, then copy the connection exactly, i.e. make sure you specify a named instance (e.g. 'VBNET\SQLSERVER'). Open SSMS, on the server node on the left side right click and select 'Properties'.
If you have SQL 2008 running on the same machine it is most likely that SQL 2008 uses the default instance and SQL 2000 a named instance.
What also could help, i.e. make testing simpler is to create an empty UDL file (e.g. 'TEST.UDL'). An empty text file should do. Then double click on it, select the provider and play around with the settings. You can always test the connection using the 'Test connection' button. I use this trick when I have to connect from a customer PC for the first time.

Troubleshooting MSSQL Connection from PHP

I'm trying to connect to an external Sql Server through PHP 5.2.
Using this line:
$con = mssql_connect('123.123.123.123','Username','Password') or die('Could not connect to the server!');
I'm receiving this error:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect
to server: 123.123.123.123 in
/home/file/public_html/structure/index.php on line 4 Could not connect
to the server!
My hosting provider assures me that ports are open for my server to connect to the DB. Looking at my php info, MSSQL Support is enabled, using FreeTDS.
Any ideas why this would be failing, or how I can begin trouble shooting the problem?
i know that there is some security for remote connection to MSSQL.
i had a same issue.
you need to give connection permition for requested ip.
did you try to connect with your personal compter with a MS SQL program.
if you can so you have problem with your cilent (php host)
if you can not so you have problem with mssql server

Categories