PHP remote connection to MSSQL failing - php

I have my first PHP project and I can connect to a SQL server database on my local SQL Server express but when I attempt to connect to a remote connection it fails. This is the connection that works locally;
$serverName = "IT90334\SQLEXPRESS, 1433";
But I have tried the following connections;
$serverName = "servername\MSSQLInstancename, 40009";
$serverName = ""server.I.P.Address\MSSQLInstancename, 40009";
$serverName = "//server.I.P.Address\MSSQLInstancename, 40009";
$serverName = "//server.I.P.Address\MSSQLInstancename";
$serverName = "server.I.P.Address"
$serverName = "server.I.P.Address:40009"
They output an error to the browser that initially states one of the following;
Provider: No such host is known
Error Locating Server/Instance Specified and
Timing out
Named Pipes Provider: Could not open a connection to SQL Server
Then the next part of the error states;
A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections
I can telnet to the server using this command
C:\telnet servername 40009
But it still won't connect. What am I missing here? Below is a section of the PHP and obviosuly I am looking for the "Connection established" to be shown in the browser
$serverName = "servername\MSSQLInstancename, 40009";
$connectionInfo = array('Database' => 'AuditDEV');
$conn = sqlsrv_connect($serverName, $connectionInfo);
if ($conn) {
echo "Connection Established.<br />";
} else {
echo "Something went wrong while connecting to MSSQL.<br />";
die(print_r(sqlsrv_errors(), true));
}

Related

How to connect SQL Server through DSN in PHP Apache ? (It's not working when Apache Service is On)

Actually I have try to connect SQL server by using sqlsrv_connect and using the DSN (Data Source Name) without Apache service then the both are perfectly working.
The problem is when I turn on the apache service then php cannot connect to the sql server using odbc DSN (Working with sqlsrcv_connect).
The condition is I need to turn on the apache with a application running using sql server which using DSN. I working with crystal report thats why really need this method. I have tried using system DSN instead user dsn, its also not working.
!
I wrote code below to test my scenario
// Connect to the data source
$conn=odbc_connect('DSNNAME','DBUSER','DBPASS');
if ($conn){
echo "Connection established DSN";
}
else {
echo "Connection using DSN Failed:" . odbc_errormsg();
}
// Connect through server name
$serverName = "WEBSERVER\SQLEXPRESS"; //serverName\instanceName, portNumber (default is 1433)
$connectionInfo = array( "Database"=>"DBNAME", "UID"=>"DBUSER", "PWD"=>"DBPASS");
$conn2 = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn2 ) {
echo "Connection established using server name";
}else{
echo "Connection could not be established by using server name";
die( print_r( sqlsrv_errors(), true));
}
The Result when Apache service is on.
Make sure the ODBC DSN matches the Crystal runtime in terms of 32-bit or 64-bit.

How to fix MySQL State [2002] Database "Connection Refused "

I am setting a new live server for my Laravel application its work perfectly on localhost but not on live server. Now its showing SQL State [2002] connection refused. I also try with mysqli_connect and PDO but the error remains the same. Is possible a problem with Hosting Provider?
<?php
$servername = "examrunner.com";
$database = "XXXXXXXX";
$username = "XXXXXXX";
$password = "XXXXXXXXXX";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Use server name as localhost. Because cPanel hosts your databases locally, use localhost as the database's hostname.
$servername = "localhost";
Please check below mentioned points to resolve this issue :
Check database and user exists
Make sure that Database user and Database connected to each other and you have given sufficient privileges to user.
If site hosted on current serve user hostname = 'localhost'
Try to print detailed error.

Connection without answer, using PHP to Mysql remotely

Hi so im trying to do a conection betwen my Apache server ( Xampp instalation ) with a remotely machine with a Mysql Server.
My php code :
<?php
$servername = '10.4.41.164:3306';
$username = 'admin';
$password = 'admin';
$db = 'AssistMe';
echo "Pepito";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
echo "Connected succesfully";
}
?>
The error that i get :
Warning: mysqli_connect(): (HY000/2002): A connection attempt failed because the connected party did not properly respond after a
period of time, or established connection failed because connected
host has failed to respond
So before puting my question here i chequed that:
If i can do a ping to the server :
Ping to the server
I added a user in mysql that allows conections from anywhere : User in Mysql
I also allow the firewall of the server to connect from the 3306 port : Firewall of the server
And the last test was to Using Mysql WorkBench and testing if i can connect to my Mysql : Mysql WorkBench
So i dont know why i cant connect to my Mysql BDD remotly from my PHP code.

Connecting to an Azure MySQL Database with PHP

Here's my code to test connecting to the DB I just set up.
<?php
// DB connection info
$servername = "slateproject.database.windows.net";
$username = "";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
and the error
Connection failed: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
Any clue what is preventing the connection?
Your database endpoint is database.windows.net, and it is the common endpoint of Azure SQL Service which is based on SQLServer but MySQL.
To connect to SQL Server in PHP, we need to enable php_sqlsrv extension, and the connection code sample:
<?php
$connectionInfo = array(
"UID" => "<user>#ttyir96emw",
"pwd" => "<your_passwd>",
"Database" => "garytestmb_db",
"LoginTimeout" => 30,
"Encrypt" => 1
);
$serverName = "tcp:ttyir96emw.database.windows.net,1433";
$conn = sqlsrv_connect($serverName, $connectionInfo);
?>
By the way, in Azure manage portal, the DASHBORAD page, we can click “Show connection string” to check the connection string and code examples in different language.
If you want to use MySQL as database, Azure provides ClearDB as MySQL server , you can follow How to Create a MySQL Database in Azure to create a MySQL database via ClearDB.
If you want to build your MySQL database in Azure VM, please refer Install MySQL on a virtual machine created with the classic deployment model running Windows Server 2012 R2.

Connect PHP on Linux server to MS SQL on remote server running Windows

Im having real trouble getting this to work. I have got the mssql-connect drivers set up and working on the Linux server, it now understands that function and does not return any errors.
I have had our server management team configure the windows server to allow the connection from the linux server. They have tested this and confirm it is all working, however I just cant seem to connect to it with PHP.
I have tried various connection strings, but it won't connect, here is an example
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = '214.133.182.71,1443';
// Connect to MSSQL
$link = mssql_connect($server, '****', '******');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
?>
I always get:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: 214.133.182.71,1443 in /home/v3rec/public_html/test.php on line 10
Something went wrong while connecting to MSSQL
The username and password of that for the MS SQL database. Ive tried it with and without the port name. I am running SQL 2012, im not sure what the instance name would be? Am I missing something? Why is PHP unable to connect?
May I suggest you use PDO and do this:
$dbhost = "myhost";
$dbport = 10060;
$dbname = "tempdb";
$dbuser = "dbuser";
$dbpass = "password";
try {
$dbh = new PDO ("dlib:host=$dbhost:$dbport;dbname=$dbname","$dbuser","$dbpass");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}

Categories