I'm trying to connect to a remote ms sql db (not a localhost), but everytime it timeouts before it sucseeds...
I'm pretty sure that the problem is the $serverName variable, is there anyway to check via Plesk Parallels what is the value of that?
<?php
$serverName = "server's ip address/database name"; //serverName\instanceName
$connectionInfo = array( "Database"=>"database name", "UID"=>"DBusername", "PWD"=>"DBpassword");
$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));
}
?>
Try to inject precise port number into ServerName like
<?php
$serverName=127.0.0.1\SQLEXPRESS,1433; //networkAddr\InstanceName,<portNum>
?>
Try using Ip address instead NetBios name if you do, cause ICMP protocol is usually blocked due security policy.
Also, the default port 1433 may be blocked. Should contact server administrator to provide actual connection requisits in that case.
The problem was that my ip address was not permitted to connect to the database, after I contancted my hosting service provider they gave me that permission and it succeeded.
Related
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.
I am trying to connect to a SQL server database with a windows account. In sql managment studio, I can open the database with this account but not with my php application. This user is not a sql user, but he have the right on the SQL database.
I tried with a sql user and it works with my php application.
Is it possible to use my windows account from the php application, and not a specifique sql user?
sqlsrv_connect
By Default it trys to establish a Connection with a Windows Auth. u can try the snipped below. If u have to use a defined WindowsUser to Login, try the Code from the documentation link above.
$serverName = "serverName\sqlexpress"; //serverName\instanceName
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"dbName");
$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));
}
There is a part of code with connection string in which you provide instance name, login, password etc for PHP to connect to MS SQL. You can change connection string on your credentials from windows domain. But its acceptable for testing but not for production :)
Another way is to connect PHP to your AD and make user to enter login/pass from domain when they come on your site. For example it is simply done in IIS (I haven't work with domain auth on PHP that runs on Linux, but as I know LDAP can help you).
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));
}
I've found myself in a Job where I have to work with a windows server (2012) - I've never had problems with establishing DB connection, but now I dont seem to find any right solution.
I'll show you my connecting php code:
error_reporting(-1);
ini_set('display_errors', 1);
$DB = array ('dbname'=>"test" , 'user'=> '***' , 'passwort'=> '***', 'host'=>'somelocalnetwork ip 192.**');
$connect = "mysql:dbname=".$DB['dbname']."; host=".$DB['host'];
try
{
$dbh = new PDO($connect,$DB['user'], $DB['passwort']);
echo "Connection established.";
$dbh = null;
}
catch (PDOException $e)
{
echo $e -> getMessage();
}
This is the result, that i get in my browser:
SQLSTATE[HY000] [2002] Es konnte keine Verbindung hergestellt werden, da der Zielcomputer die Verbindung verweigerte.
translated into english :
No connection could be made because the target machine actively refused it .
NOTE:
I downloaded mssql drivers and sqlsrv drivers and extracted them to the /ext/ direcoty , included them in the php ini file.
But when checking the php_info() i dont see any mssql nor sqlsrv parts.
I don't know if thats relevant
The Windows Server is set as WebServer and as normal Microsoft SQL Server
To connect to sql sever using sqlsrv:
<?php
error_reporting(E_ALL);
$serverName = "SuperComputer-PC";
$connectionInfo = array('Database'=>'RiverDatabase', "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if($conn) {
"Connection established.<br />";
}else {
"Connection could not be established.<br />";
die(print_r(sqlsrv_errors(), true));
}
?>
Download php SQL Server from:
http://www.microsoft.com/en-za/download/details.aspx?id=20098
and copy the relevant drivers to your php ext folder.
add the extensions to the php.ini file, for example:
I am using 5.4, but this should work for 5.5, I am not sure if a driver exists for 5.6 yet, but if it does, that's great.
it might be 1) Authorization of pc needed Or
2)port number(lesser chances).
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Or Try
try
{
if ($db = mysqli_connect($hostname_db, $username_db, $password_db))
{
//do something
}
else
{
throw new Exception('Unable to connect');
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
For Connection Problem:
If this happens always, it literally means that the machine exists but that it has no services listening on the specified port, or there is a firewall stopping you.
If it happens occasionally - you used the word "sometimes" - and retrying succeeds, it is likely because the server has a full 'backlog'.
When you are waiting to be accepted on a listening socket, you are placed in a backlog. This backlog is finite and quite short - values of 1, 2 or 3 are not unusual - and so the OS might be unable to queue your request for the 'accept' to consume.
The backlog is a parameter on the listen function - all languages and platforms have basically the same API in this regard, even the C# one. This parameter is often configurable if you control the server, and is likely read from some settings file or the registry. Investigate how to configure your server.
If you wrote the server, you might have heavy processing in the accept of your socket, and this can be better moved to a separate worker-thread so your accept is always ready to receive connections. There are various architecture choices you can explore that mitigate queuing up clients and processing them sequentially.
Regardless of whether you can increase the server backlog, you do need retry logic in your client code to cope with this issue - as even with a long backlog the server might be receiving lots of other requests on that port at that time.
There is a rare possibility where a NAT router would give this error should it's ports for mappings be exhausted. I think we can discard this possibility as too much of a long shot though, since the router has 64K simultaneous connections to the same destination address/port before exhaustion.
I am trying to establish a connection to my company's MSSQL server with windows authentication , but it fails as it is trying to use my computer name as login instead of my login id. When logging in with the MS SQL Server Management the windows authentication works fine, but not with this PHP code:
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$serverName = "xxx";
$connectionInfo = array( "Database"=>"xxx");
/* Connect using Windows Authentication. */
$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 printout I get is the following:
[Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for
user 'xxx\T2002197$'. ) )
T2002197 is my computer name, not my login id, so of course it fails. How can I solve this? I am using WAMP.
Edited out some info, replaced with 'xxx'
Aah problem solved! I changed the settings on my WAMP service (open service.msc in Windows) and made sure the service logged on the correct account. It works now.
The manual says that per default the connection is using Windows Authentication and not the SQL Server authentication.
To bypass this you need to provide the uid and pwd options in your $connectionInfo data.
Se the manuals;
http://php.net/sqlsrv_connect
http://msdn.microsoft.com/en-us/library/ff628167.aspx