Here's the code that I am using.
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$myServer = "ip-address:1334/SQLEXPRESS";
$myUser = "username";
$myPass = "password";
$myDB = "dbname";
$link = mssql_connect($myServer, $myUser, $myPass);
if ( !$link ) {
if ( function_exists('error_get_last') ) {
var_dump(error_get_last());
}
die('connection failed');
}
?>
Now this code is running on a linux machine and the server is on a windows server. The IP is correct as well as username, password I created a new pair inside SQLEXPRESS. However, I still get a connection issue.
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server
the port 1334 is opened by our windows server and SQL server is listening to both 1334 and 1433. THey wont open default port for security reasons. They have double checked the settings but I still cannot connect.
What should be the next action for me.
Kind regards
Khuram
May be the windows SQL server have a firewall restriction which is restricting to access from outside of the server.
For connecting Linux + Apache + SQLEXPRESS 2005 take care about it:
Don´t use the standard MS-SQL port (1433), use the MS-SQL dynamic port under SQL Server Configuration Manager -> SQL Express Protocols -> TCP/IP properties -> IP Adresses -> IPAll
You can do a direct connection (without FreeTDS) using the following statemt:
$db=mssql_connect('192.168.xxx.xxx:1541','usrxxxx','pwdxxxx');
-You can use FreeTDS configuring the freetds.conf file as follow:
[connect2k5]
host = 192.168.xxx.xxx
port = 1541
tds version = 8.0
With the following PHP statement:
$db=mssql_connect('connect2k5','usrxxxx','pwdxxxx');
I have got it from http://php.net/manual/en/function.mssql-connect.php
You can also try http://www.akamarketing.com/blog/99-php-sql-server-connection-problems-mssql_connect-functionmssql-connect-unable-to-connect-to-server.html
The port may not be open. You should be able to connect via telnet to port 1334 or 1433. You won't get any text back, but it will connect.
Related
I was trying to use php to remote connect to MSSQL database in other server. But at the end it show error
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: xxx.xxx.xxx.xxx in xxxxxxx
Here is my code
<?php
$myServer = "xxx.xxx.xxx.xxx";
$myUser = "user";
$myPass = "password";
$myDB = "database";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
?>
I have try the login with microsoft SQL Server Management Studio and it can work and login so I don't that having problem with the allow remote connection access.
Btw.. I just access it from XAMPP Localhost. I'm not sure is this effect or not.
Thank you in advanced for whose help.
First guess:
Your SQL-Server is not listening to TCP/IP. Enable that protocol in configuration manager. Restart the server process.
Second guess:
A firewall is blocking the access. Enable access to TCP Port 1433.
Third guess:
driver problems
mssql driver need that special version of the ntwdblib.dll.
Depending on you php version it's not there.
Better switch to the new driver "sqlsrv".
https://www.microsoft.com/en-us/download/details.aspx?id=20098
I try to connect my android application using JSON Parser to the web hosting. But whenever I try to connect (even just open using url in the browser) I will get the error message.
<?php
$dbHost = 'http://sql4.000webhost.com/localhost';
$dbUser = 'a6410240_cbetTD';
$dbPass = 'xxxxxx';
$dbName = 'a6410240_cbetTD';
$conn = mysql_connect ($dbHost, $dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($dbName,$conn);
?>
This is my database.php file. The full error message is
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'http' (4) in /home/a6410240/public_html/database.php on line 8.
I have tried change the $conn but still it didn't worked for me.
Thanks
If your database and application is on same server then use "locahost" in $dbhost.
And if your database and application is on different servers then you need to use IP address or hostname in $dbhost and the database user should be added on database server provided with required privileges.
The problem you are having was already mentioned in one of the comments, this one to be precise.
For your solution to work, all you need to do is omit the part http:// at the beginning and probably /localhost at the end.
The host is only the domain you are referring to. In this case sql4.000webhost.com. With /localhost you tried to already connect to a database, although your configured database is supposed to be a6410240_cbetTD.
MySQL use TCP port 3306 by default
($dbport) and hostname or IP address ($dbhost). For LAMP (Linux-Apache-MySQL-php) you can find a lot of tutorials.
Usually MySQL server listens internal port (which can't be reached via Internet) for security purposes.
If you familiar with docker, you can simply download examples of LAMP solutions from hub.docker.com.
I have tried to connect to DB2/AS400 remote database with db2cli odbc module and odbc_connect but returns the next error:
Execution failed: [unixODBC][IBM][CLI Driver] SQL30081N A
communication error has been detected. Communication protocol being
used: "TCP/IP". Communication API being used: "SOCKETS". Location
where the error was detected: "10.10.100.5". Communication function
detecting the error: "recv". Protocol specific error code(s): "",
"", "0". SQLSTATE=08001
This is my PHP code:
<?php
$database = 'xxxx';
$user = 'xxxx';
$password = 'xxxx';
$hostname = '10.10.100.5';
$port = '55000';
$driver = 'DB2';
$conn_string = "DRIVER={$driver};DATABASE=$database;HOSTNAME=$hostname;PORT=$port;PROTOCOL=TCPIP;UID=$user;PWD=$password;";
if (!$db = odbc_connect ($conn_string, $user, $password)) {
print("Execution failed:\n");
}
else echo 'Success!';
odbc_close($db);
Help me please!!
I am not familiar with AS400, but on Linux/UNIX this error happens when the TCP/IP communication has not been set properly at the server side. There are two things I would check (on a Linux/UNIX server, not sure about the AS400 equivalents):
netstat -na
db2set DB2COMM=TCPIP
The first command will return the list of open TCP/IP listeners. Your port 55000 should be on the list. If it is not, it means the server is not open for communication.
The second part enables TCP/IP communication in DB2. Note that there are dependencies on /etc/services and the SVCENAME DBM CFG parameter. The server must be restart for DB2COMM to take effect. When enabled properly, DB2 should start listening at the configured port.
If none of this helps, then it is possible that a firewall is blocking access to the DB2 server.
Again, I apologize if this reply does not apply to AS400.
When trying to connect to remote Mysql server I get this error :
php_network_getaddresses: getaddrinfo failed: Hôte inconnu. (translation: Host unknown).
con.php :
<?php
$db_host = 'http://xxx.xxx.xxx.xxx';
$db_username = 'xxx';
$db_password = 'xxx';
$db_name = 'xxx';
$con = mysql_connect($db_host,$db_username,$db_password);
$select_db = mysql_select_db($db_name,$con);
?>
When connecting to localhost all goes fine, but for remote connection it didn't work.
$db_host = 'xxx.xxx.xxx.xxx'; Instead
You can't use the protocol HTTP for Mysql since they both run on different ports / use different protocols.
Further more HTTP (web service) = 80 Mysql (your database) = 3306 just to note as mentioned by BenM this is the default port. In most instance this is not changed. However you change its port to any other port (like you can with any other service).
And in addition please take a look at PDO since Mysql_ functions are going to be deprecated and are also dangerous to use in new systems and in old ones as well.
Take a look at one of my answers on how to set up PDO
Edit 1
As you mentioned on benM's answer in a comment, "No connection could be made because the target machine actively refused it."
so now if you have root access on the actual server the database is hosted on then you have to check if the server (Example in my case is ubuntu server)
run this command :
sudo netstat -pan | grep mysql
Also take a look at this picture : Pretty much it shows the TCP port, if it iS Listening or not listing (here it is listening)
again this is assuming you have root access to the actual linux / (whatever server) I am only proficient at ubuntu but can assist you with other server information as well.
You cannot connect to MySQL using the HTTP protocol (namely because of port conflicts). The parameter should be an IP string. Remove the http protocol as follows:
$db_host = 'xxx.xxx.xxx.xxx';
$db_username = 'xxx';
$db_password = 'xxx';
$db_name = 'xxx';
$con = mysql_connect($db_host, $db_username, $db_password);
$select_db = mysql_select_db($db_name, $con);
You should also note that MySQL can be configured to prevent anything other than localhost access on user accounts. Ensure that the user you're using is able to access the database from any domain.
Finally, the mysql_* family of functions are now being deprecated, and you should avoid using them for new code. Instead, check out MySQLi or PDO.
I've had to move an app we wrote for a client to a new server and a remote connection I was initiating with PHP mssql_connect has ceased to work. I noticed that PHP wasn't compiled with mssql so I asked the server admin to install it. I can verify that it's now installed via PHP info but I now get a consistent "Unable to connect to server" error from mssql_connect.
Here's the very simple PHP script I'm running:
$myServer = "myserver.com:5000";
$myUser = "myusername";
$myPass = "mypassword";
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer. Error: " . mssql_get_last_message());
I've confirmed that the credentials are still correct but for whatever reason it seems that mssql_connect just isn't doing it's thing. I'm wondering if there's something that the admin has forgotton to do having installed the extension and FreeTDS. Any pointers greatly appreciated! :)
PROBLEM SOLVED!!!
After all that it turned out to be the FreeTDS protocol version number as specified in /usr/local/freetds/etc/freetds.conf, line number 13 had to be uncommented. That was it! :)
After all that it turned out to be the FreeTDS protocol version number as specified in /usr/local/freetds/etc/freetds.conf, line number 13 had to be uncommented. That was it! :)
There's a good chance that remote connections are disabled on the server where the server is installed. Have you checked whether you are actually able to connect to the DB server remotely, from the IP address of the webserver that attempts to connect to it? Usually, at least in most shared hosting schemes, the database server only accepts connections from localhost. You'd need to add the IP address of the webserver to a whitelist (usually in the server's configuration) to allow connections from the outside.