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

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

Related

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.

PHP remote access MSSQL Server

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

connect to mysql database using php on different port

my connection string is:
$connection = mysql_connect("localhost", "root", "") or die("Could Not Connect to DB: ".mysql_error());
but on my computer i have installed IIS server first so localhost:80 is reserved by it so I changed my port to 8080 changing httpd.conf file in apache folder.
Now what should I change in connection string to connect to my database.
I am using XAMPP .
should I tried writing these:
1:
$connection = mysql_connect("localhost:8080", "root", "") or die("Could Not Connect to DB: ".mysql_error());
2:
$connection = mysql_connect("localhost,8080", "root", "") or die("Could Not Connect to DB: ".mysql_error());
But Not working Help please.
Thank You
Default port number which Mysql server uses is 3306
Web servers default port is 80
Web server and Mysql server are independent.
Do not change your api to connect to your mysql

Connect to SQL server 2014 management studio with PHP

Is there a way to connect to Microsoft's SQL server 2014 management studio with PHP?
I was able to connect to the same database with ASP.NET, but I'm not seeing anything online on how to connect to that type of database with PHP. All I'm seeing is Mysql, not SQL server 2014 management studio with PHP.
this is what I have so far to connect via PHP
<?php>
$myServer = "LALA";
$myDB = "LGWeb";
// connection to database
$dbhandle = msql_connect($myServer)
or die("couldn't connect to SQL Server on $myServer");
// select database to work with
$selected = mssql_select_db($myDB,$dbhandle)
or die("couldn't connect to database - $myDB");
if (isset($_POST['submitted'])){
include('')
}// end of main if
?>
I changed my database names and server name in code above. But that's the basic idea. Just trying to connect to it with PHP.
This is how I connected to it with ASP.NET
<connectionStrings>
<add name="LALAConnectionString" connectionString="Data Source=LALA;Initial Catalog=LGWeb;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

Unable to connect to msSQL database via PHP

I am using the current code in attempt to access a msSQL 2005 db:
<?php
$myServer = "[server]";
$myUser = "[username]";
$myPass = "[password]";
$myDB = "[db]";
//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");
//declare the SQL statement that will query the database
$query = "SELECT id, name, year ";
$query .= "FROM cars ";
$query .= "WHERE name='BMW'";
//execute the SQL query and return records
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
//display the results
while($row = mssql_fetch_array($result))
{
echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>
It is returning the following:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: XXXXXXX in D:\xxxxx.xxx\xxxx.php on line 16
Couldn't connect to SQL Server on XXXXXXX
What do you think the problem is?
It sounds to me like one of your DLLs is the wrong version. There was an issue of some sort with the move from SQL2000 to SQL2005 that the creators of PHP didn't resolve themselves. There are a variety of posts about it here: the following link
I believe the DLL is ntwdblib.dll and the version needs to be version 2000.80.194.0 at the very least. If you're running Apache or WampServer, there is an identical dll where the Apache DLLs are stored that needs to be overwritten.
Note: I was having this issue a few days ago and finding the correct DLLs and overwriting both allowed it to work.
Also: You may need to setup remote connections. Sql Server 2005 has remote connections disabled by default. You can allow remote connections by running the SQL Surface Area Configuration utility.
Try calling mssql_get_last_message() to get the last error message:
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer. Error: " . mssql_get_last_message());
stop using
mssql_connect
and start using
sqlsrv_connect
That will save you a lot of headaches. Plus, the function *mssql_connect* has been deprecated.
For using sqlsrv_connect, you must download the driver and install it as an extension for PHP to recognize the sqlsrv functions. Download the driver from Microsoft Download Center (search for "sql server php driver"), at the time of this post the download url was: http://www.microsoft.com/en-us/download/details.aspx?id=20098
The right steps for installation are clearly explained by Microsoft itself at http://www.microsoft.com/en-us/download/details.aspx?id=20098
download the driver. 2. put it in the PHP ext folder. 3. update the php.ini 4. restart the server.
After installing the sql server driver, then simply follow the instructions from http://www.php.net/manual/en/function.sqlsrv-connect.php. Below a quick snippet:
<?php
$serverName = "serverName\sqlexpress"; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// 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));
}
?>
Vote up!
Invalid credentials, if your not using localhost be sure you add the server's ip to the safe list.
I had some difficulties with this a few months ago, and I found that the only way to make it work was to include the instance name when specifying the server. For example:
$myServer = "SERVER\INSTANCENAME";
Specifying just the server would not work, even with TCP/IP enabled.
First try to do something like phpinfo() on your browser, than see which databases are allowed.
If you don't see anything like mssql then it is not configured. So use the odbc_connect() which will be configured...your connection string will be like this:
$server = '';
$user = '';
$password = '';
$database = '';
$connection = odbc_connect("Driver={SQL Server Native Client `11.0};Server=$server;Database=$database;", $user, $password);`
If you are using mssql 2005 or 2008, then change d 11.0 to 10.0 and for other versions just change to the version of your mssql.
Download the driver in this site http://www.microsoft.com/en-us/download/details.aspx?id=20098, then open your php.ini file and add the DLL that you download.
Late response, but it might help someone.
we are nearly there, it is the problem with your connection parameters,
probably you need to give servername as IP:port
servername:: The MS SQL server. e.g. hostname:port (Linux), or hostname,port (Windows).
Server Name should be "server\instance"
An instance is nothing more than specific port address different than 1433... so just discover the port on mssql server and then try to connect using: ip:port
Example code that works completely fine for me::
ini_set('display_errors', '1');
// $myServer = "winsrv22.somedns.co.uk:22320";//this style works as well
$servername = "123.21.47.23:22320";
$myUser = "UserName";
$myPass = 'xxxxxxxx';
$myDB = "[database]";
//connection to the database
$dbhandle = mssql_connect($servername, $myUser, $myPass)
or die("Couldn'tt connect to SQL Server on $myServer");
if($dbhandle) {
echo "Success, Connected\r\n";
} else {
echo "problem :( \r\n";
}
Hope this helps some one :)
Happy coding!!
if you using windows 10.
step 1. click start button
step 2. type Service and enter (open App Services)
step 3. find - SQL Server (MSSQLSERVER)
step 4. Right click ->select Properties
step 5. show popup. you select 2nd tab (Log On)
step 6. select local system account
step 7. and check allow service to interact with desktop
step 8. finally click ok.
1 time refresh
now connected.
I hope it's very useful

Categories