PHP remote access MSSQL Server - php

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

Related

MySQL connect failed. Can't connect to MySQL server on 'http' (4)

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.

(php) could not connect to the MSSQL 2008 on the vps server

I have a google vps server with a database MSSQL 2008 r2 . on a computer at home I can not connect to the database with php script but when filled ip,port tool "SQL Server Management Studio" it connects successfully . please help me fix it, I seem to be going crazy :(
Image SQL Server Management Studio 2008 r2
here is my php code
<?php
$myserver = "xx.xx.xx.xx,1433";
$myuser = "sa";
$mypass = "123456";
$mydb = "muonline";
//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");
//close the connection
mssql_close($dbhandle);
?>
I tried to change $myserver with the value:
xx.xx.xx.xx:1433
xx.xx.xx.xx\\instance-1,1433
xx.xx.xx.xx:1433\\instance-1
xx.xx.xx.xx
xx.xx.xx.xx\\instance-1
always notify Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server

Access a MySql Database from PHP (not on localhost)

there's this server:
http://phpmyadmin.pvdata.fr/index.php ,
I can connect to it knowing the username and password and Server Choice.
Once I connect to it, it shows under MySql the following information: (I'm Translating from French)
Server: sql6 (sql6 via TCP/IP)
Server Version: 5.0.92-87
Protocol Version: 10
Username: pvdata#10.5.1.3
Character for MySql: UTF-8 Unicode (utf8)
So I tried using the following code in order to connect to the Database:
<?php
// Create connection
$username = "pvdata";
$password = "xxxxxxxx";
$hostname = "10.5.1.3";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
?>
But it didn't work, it's saying: "A connection attempt failed because the connected party did not properly respond after a period of time".
What am I doing wrong?
Go to your phpMyAdmin installation and look for the file config.inc.php. Open that file, and check which hostname / ip is being used for SQL6. That is the IP you should use for your mysql_connect()
This is most likely a firewall issue. Most 3rd party hosts don't allow you to access MySQL from 'the outside'.
You can do this if you manage the MySQL server yourself or if you have a host that doesn't deny you to access the database from another machine, but in regular web hosting, this is not common.
You need to search, google helps a lot, also you need to connect to an ip/domain and port of your MySQL server.
For this, you need to know what is the port of your mysql server.
Look this answer from link
<?php
mysql_connect("mysite.com:3306", "admin", "1admin") or die(mysql_error());
echo "Connected to MySQL<br />";
?>
to check if you have coneccion, try in console:
shell> mysql --host=remote.example.com --port=3306 //3306 is the
defaul port for MySQL
http://dev.mysql.com/doc/refman/5.0/en/connecting.html

My code only connect to localhost mysql server but not for remote server

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.

php mssql_connect not working

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.

Categories