Unable to connect to MSSQL database in a different domain - php

HI friends I wants to connect local server (linux) to a remote desktop's mssql database. I have reviewed the php document for assistance.
It defines the use of mysql_connect().
I used that and ends with some issues mainly do to the difference in domain.
Here is the datas I have.
$dbname = 'dbname';
$servername = 'servername.abc.cbd.net\instancename';
$username = 'domain\username';
$password = 'password';
$port = '1433';
Here is the connection code I have used.
$link= mssql_connect($servername, $username, $password);
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
Error am getting is
Warning: mssql_connect(): Unable to connect to server:servername.abc.cbd.net\instancename
Please help me to correct the code. Is there any other alternative for myssql_connect().
Thanks in advance.

Related

AWS PHP PDO MySQL Connection Problem SQLSTATE[HY000] [2002]

I am trying to connect to my AWS MYSQL database following this tutorial: https://docs.aws.amazon.com/zh_cn/elasticbeanstalk/latest/dg/create_deploy_PHP.rds.html#php-rds-connect.
My Code
<?php
$dbhost = $_SERVER['RDS_HOSTNAME'];
$dbport = $_SERVER['RDS_PORT'];
$dbname = $_SERVER['RDS_DB_NAME'];
$charset = 'utf8' ;
$dsn = "mysql:host={$dbhost};port={$dbport};dbname={$dbname};charset={$charset}";
$username = $_SERVER['RDS_USERNAME'];
$password = $_SERVER['RDS_PASSWORD'];
$pdo = new PDO($dsn, $username, $password);
?>
The output
SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
I am able to connect to my database with MySQL Workbench.
Any help would be greatly appreciated!
Please verify your Database settings. May be it's a wrong port setting.If you use localhost you can try to change the host to 127.0.0.1. The MySQL process might be stuck.

Access local MySQL server within my network

I have a website that has a page that needs to show all the information that is in the local MySQL server (not in the hosting). How am I gonna do that?
I only tried to change the connection details but no luck on that matter.
db_connect.php (connect to hosting server)
<?php
$servername = "localhost";
$username = "xxx";
$password = "yyy";
$database = "dbdb";
$conn = new mysqli($servername, $username, $password, $database);
if($conn->connect_error){
die("FAILED TO CONNECT : " . $conn->connect_error);
}
?>
to db_local.php (connect to my local server)
<?php
$servername = "192.168.x.xx";
$username = "";
$password = "";
$database = "info";
$conn = new mysqli($servername, $username, $password, $database);
if($conn->connect_error){
die("FAILED TO CONNECT : " . $conn->connect_error);
}
?>
Is it possible? I only need to do that as a first aid solution in my main problem. I am getting this kind of error:
FAILED TO CONNECT : Can't connect to MySQL server on '192.168.x.xx' (110)
mysql might only accept connections from localhost, if you are connecting to the db from your local computer then you need to change the address to localhost
If you are not connecting to the DB via local machine then you need to make sure that port 3306 is open on the server, selinux is off ( assuming you are on linux ) and that you have the proper credentials.
If that doesnt work edit your my.cnf and change the line bind-address: 127.0.0.1 to the machines local ip.
Hope this works...
https://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
connecting to mysql server on another PC in LAN

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;
}

Up until this morning my mysqli_connect was working perfectly

But now, I'm getting an error: Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2005): Unknown MySQL server host '
I can get to the mysql database on the server and Godaddy ran scripts that work. They said my parameters were correct but they say it's on my end. I'm talking directly to their server not a localhost. I tried it on my smart phone with a different IP and got the same message. If it's not them and not my code, what can it be? The error is on the $conn = line.
$hostname = "AAAA.server.net";
$username = "BBBB";
$dbname = "CCCCC";
$password = "XXXXX";
$usertable = "tblOne";
//Connecting to database
$conn = mysqli_connect($hostname, $username, $password) OR DIE
("Unable to connect to database! Please try again later.");
mysqli_select_db($conn, $dbname);
Be sure and confirm your database host, because the error message is talking of host problem
Unknown MySQL server host
With GoDaddy, your domain name may not be where your MySQL server resides. It depends upon your server type and your hosting plan. GoDaddy has a help page that will point you in the right direction:
https://support.godaddy.com/help/article/39/viewing-your-database-details-with-shared-hosting-accounts

PHP mysqli connection to remote database

Trying to connect to a database that is not on the same host as my site for the first time and not having much luck. The DB is on a godaddy host and I have configured it to allow remote access:
$dbc = mysqli_connect($db_host,$db_user,$db_pass,$db_name);
if (!$dbc) {
die('Connect Error: ' . mysqli_connect_error());
}
and I get the following on page:
Warning: mysqli_connect() [function.mysqli-connect]: [2002] Connection
timed out (trying to connect via ‘mydbhostname:3306) in ‘path’ on line
3
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002):
Connection timed out in ‘path’ on line 3
Connect Error: Connection timed out
Anything I need to look for on my end or something in the script that could be causing this?
EDIT: well it appears everything works when I use mysql, but not mysqli...and mysqli is enabled on the server.
I had the same problem on a Digitalocean Ubuntu 14.04 server, where the firewall configuration was the culprit. Mysql wasn't allowed on port 3306 by the ufw firewall rules.
Solution:
$ sudo ufw status
$ sudo ufw allow mysql
$ sudo ufw reload
Make sure that you have inserted correct hostname for your database, here you can see how to do it on godaddy hosting:
http://support.godaddy.com/help/article/4978/connecting-remotely-to-shared-hosting-databases
http://support.godaddy.com/help/article/39/locating-your-databases-host-name?locale=en
I don't understand the difference between mysqli and mysql, but I've found on GoDaddy that using:
mysqli_connect doesn't work.
mysql_connect does work.
Try using the following sample code to see if you can connect to your database which needs data in at least one table:
<?php
//Variables for connecting to your database.
//These variable values come from your hosting account.
$hostname = "XXXXXXX.db.XXXXXXXX.hostedresource.com";
$username = "XXXXXXX";
$dbname = "XXXXXXX";
//These variable values need to be changed by you before deploying
$password = "XXXXXXX";
$usertable = "XXXXXXX";
$yourfield = "XXXXXXX";
//Connecting to your database
mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname);
//Fetching from your database table.
$query = "SELECT * FROM $usertable";
$result = mysql_query($query);
if ($result) {
while($row = mysql_fetch_array($result)) {
$name = $row["$yourfield"];
echo "<h2>some data</h2>";
echo "$name<br>";
}
}
?>

Categories