PHP Sqlsrv connection not working when specify port, instance - php

PHP version:7.4, SQL Server version:2019,
I was going to try to specify port in connection but it didn't work.
<?php
$serverName = "MYPCNAME, 1433";
$connectionInfo = array( "Database"=>"MyDB", "UID"=>"sa", "PWD"=>'MyPassword');
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
Above showed below message.
[Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: An existing connection was forcibly closed by the remote hos
When I removed port like this $serverName = "MYPCNAME" , it worked well.
I changed port to 1434, and tested again (Of course I restarted SQL Server, firewall set).
$serverName = "MYPCNAME, 1434"
This gave me same error but $serverName = "MYPCNAME" This worked well.
I tried to specify instance name too, but there was same issue.
I installed php_sqlsrv, php_pdo_sqlsrv well and in phpinfo(), they are displayed as installed successfully.
Is there anything what I missed?
Can anyone help me?

Related

PHP script to connect to SQL Server

I am running this script
<?php
$mysqli = mysqli_connect("ServerName", "username", "password", "dbname");
$res = mysqli_query($mysqli, "SELECT * from table1");
$row = mysqli_fetch_assoc($res);
echo $row['_msg'];
?>
I am getting this error:
mysqli_connect(): (HY000/2002): No connection could be made because
the target machine actively refused it
I have looked around online, but I can't figure out what is wrong. All the credentials are correct and it still doesn't work.
I am connecting to a local db on SQL Server through SQL Server authentication. Do I need MySQL running on xamp for this to work? Any ideas?
I am using Windows 10 through bootcamp on a mac. This is for SQL Server, not for mysql
It seems that you want to connect to a Microsoft SQL Server database. In this case maybe this code helps:
<?php
$serverName = "serverName\\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
Also, if you run your php server locally with XAMPP you need to have Apache activated. If you try to connect to a MS SQL Server database you dont have to have MySQL enabled since you are not trying to connect to a MySQL database.
Source

Connect to MS SQL Server with PHP without drivers

A project at work has come up that requires me to connect to a Microsoft SQL Server 2014 in PHP. I have found tons of solutions online that involve downloading a set of drivers to either use PDO, sqlsrv, or ODBC.
After contacting my hosting provider this is what they said:
As you're on shared hosting, drivers can't be installed as it's not
supported on our platform.
You can use normal ODBC to connect if required
I so far haven't been able to find a solution that works without having to install any drivers/extensions. The hosting provider recommended ODBC but I can't seem to get my code to work for ODBC either. Does anyone know any other way I would be able to do this?
The code I'm currently using (which gives me an error:
Can't open lib 'SQL Server'
$server = 'xxx.xxx.xxx.xxx';
$user = 'user.....';
$pass = 'password...';
$port='1433';
$database = 'database...';
$connection_string = "DRIVER={SQL Server};SERVER=$server;PORT=$port;DATABASE=$database";
$conn = odbc_connect($connection_string,$user,$pass);
if ($conn) {
echo "Connection established.";
} else{
die("Connection could not be established.");
}
Have you tried this?
<?php
$serverName = "serverName\\sqlexpress, 1542"; //serverName\instanceName, portNumber (default is 1433)
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
Refer: https://www.php.net/manual/en/function.sqlsrv-connect.php

connect to SQL Server Database with PHP

I want to connect to a specific database on SQL Server Through PHP. On WAMP Server, I have PHP 5.6 installed so I downloaded SQLSRV32.EXE (Microsoft Drivers for PHP for SQL Server) then I copied these files in C:\wamp64\bin\php\php5.6.25\ext
IN php.ini, I added
extension=php_sqlsrv_56_nts.dll
extension=php_sqlsrv_56_ts.dll
But when i check phpinfo() on browser i don't see the sqlsrv module listed.
When i try to connect to my SQL Server Database I get this error
Fatal error: Call to undefined function sqlsrv_connect()
Here is my code
<?php
$serverName = "PC0CFEP2\SQLEXPRESS"; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"test");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
I was checking my solutions on different forums and I tried many but I still can't get it work. I am sure it's in some settings but i couldn't find out what could be the issue.
Any suggestion please ? Thank you very much.
At First, Please run MSSQL server database.
In php.ini file, Please added below code
extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_56_ts.dll
and keep two dll file \ext folder
To connect database , try
$serverName = "serverName\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
Remove "php_" prefix:
extension=sqlsrv_56_ts.dll
extension=pdo_sqlsrv_56_ts.dll
This worked for me. Thanks for asking this question.

PHP/MSSQL Connection

I have the following code that doesn't seem to make the connection to the MSSQL db:
<?php
$serverName = "SEVER_NAME\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"TESTDB",
"UID"=>"SERVER_NAME\Administrator",
"PWD"=>"Password123");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
echo "Connection established.<br />";
}
else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
The message reads that login failed for user SERVER_NAME\Administrator, but that's what I see in the SQL Server Management Studio when I check the DB properties. Any help is greatly appreciated.
The login name looks suspiciously like that SQL server is using Windows authentication. If this script and the SQL server are on the same machine, try omitting UID and PWD entirely, as per the documentation.
If values for the UID and PWD keys are not specified in the optional $connectionInfo parameter, the connection will be attempted using Windows Authentication.

Cannot connect to SQL Server on local machine using PHP

I wonder if anyone can help me. I'm creating an application in PHP and am using a SQL server database on my local computer to develop the application. The only problem is I cannot connect to the database. My code looks like:
<?php
$serverName = "LIAMJAY-PC\SQLEXPRESS"; //serverName\instanceName
$connectionInfo = array( "Database"=>"ONEDB");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
to connect to the database. However, everytime I run the code I keep getting a 18456 error code. Does anybody know what my problem is and if so, what is the solution???
Try supplying UID and PWD as follows:
$connectionInfo = array( "Database"=>"ONEDB", "UID"=>"userName", "PWD"=>"password");
Error code 18456 occurs when you are using SQL Server Authentication and the login name and password are incorrect, or even when the user is disabled or locked.

Categories