Connect to SQL server from PHP via odbc connection with Windows authentication - php

I'm trying to connect to a local SQL server from a PHP srcipt using odbc_connect, but I can't log in.
This is my code so far:
$user= "PC_NAME\PC_USER";
$password="";
$dsn = "Driver={SQL Server};Server=MyServer;Database=MyDatabase;Integrated Security=SSPI;Persist Security Info=False;";
$link = odbc_connect("$dsn", $user, $password);
Can you see anything wrong?

I had a bad configuration in my SQL Server. I had to allow SQL login besides the default Windows login

Related

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

get request timeout PDO connection

I'm trying to connect to remotemysql on another server and i've all the authentications and these authentication are working well to connect to remote sql in VISUAL STUDIO and PHP STORM but when i open up the url royalresidency.pk/administration and try to login and it gives me this error i don't understand this error i tried to allow that from from remotesql also and also this ip is allow but still i can't get access to sql from this server what can be the issue ?
$connect = new PDO('mysql:host=65.60.37.227;dbname=cricflip_RoyalResidency',$dbuser, $dbpass);
return $connect;
Try to check your mysql connection. Try connecting with command line. (Mysql should be preinstalled in your system)
mysql -u USERNAME -pPASSWORD -h REMOTE_SERVER_IP DB_NAME

mysql_connect(): Connection refused

I was just wondering if it was possible for certain databases to block SQL accesses through mysql_connect. I recently downloaded XAmpp and created a SQL database using my own computer as a local host and wrote a PHP file that accessed that database.
However, when I tried to change the username and host settings so that Xampp would run the PHP files that would then connect to the external SQL database of a free hosting site that I'm trying to use (biz.co.nf), I got the following error message:
Warning: mysql_connect(): Connection refused in /Applications/XAMPP/xamppfiles/htdocs/...
Right now I'm thinking either:
My login credentials to that database are incorrect, or
Somehow the host blocks SQL accesses from external users, but if I were to load my php code into the server using FileZilla, it should work okay?
Please let me know if this is the case.
I'm also fairly certain I have the right login credentials.
Here's what I have:
$host = "fdb13.biz.nf";
$username = "1764941_login";
$password = ________;
$db_name = "1764941_Login";!
$tbl_name = "Members";
//Connect to server
mysql_connect($host, $username, $password)or die("Cannot Connect!");
mysql_select_db($db_name)or die("Cannot select Database");
with my server settings according to my website...
Of course, I omitted my password.
Connections are refused from remote locations to MySQL for security reasons. You can add your IP Address to enable MySQL to listen from your database by following the instructions in this link - Cyberciti biz tips on enabling remote access to mysql database server In your case, add the IP address of the location where your PHP script.
I would also say that
mysql_connect
is deprecated in PHP. Use mysqli instead. Check the comments in the link - PHP original MySQL API

ODBC Connection with integrated security PHP and MS SQL Server

I need the guidance help on ODBC connection string in PHP, which I am using to connect to MS SQL Server.
<?php
$server = 'UKEMO03';
$database = 'mtpFetch';//the database to connect to
$user = 'shoabg';// the user has PERMISSIONS AT THE DATABASE
$pass = 'Shsx12x';//and here the user's password
$dsn = "Driver={SQL Server};Server=$server;Database=$database;";
$connect = odbc_connect($dsn, $user, $pass);
?>
Above code works treat, but i have a requirement of not to specify the username and password and use the connection string with Integrated Security. I can't find anything on internet and I can't change the connection string method because I've completed most of my work and by adapting another connection method will require a massive change in a code in theory its not an option for me.
Is there a way we can create Integrated Security connection to connect MS SQL Server in PHP using ODBC
Please help i don't know what to do
This will connect you to the ODBC master database (cathalog).
I did not succeed to connect to another database. There is an option for default database in the configuration of ODBC SDN, but has no effect
("Chenge the default database to").
$this->dbHandle = odbc_connect("Driver={SQL Server};Server=localhost;Integrated Security=SSPI", '', '');
If you use PHP ini make sure you use:
mssql.secure_connection=On
On the database server, make sure you have Windows Authentification associated with your login username

linux php connect to Server 2008 running MS SQL Server Express

i can connect from client PC to SQL Server Express using the following creds. in mgnt sudio:
server: 192.168.44.96\sqldb
auth: windows auth
user name: domain\user
pw: 1234!
Have a LAMP server trying to connect to the above SQL server, here's what the PHP looks like:
$server = '192.168.44.96\sqldb';
$username = 'domain\user';
$password = '1234!';
$database = 'testing123';
$connection = mssql_connect($server, $username, $password);
if($connection != FALSE){
echo "Connected to the database server OK<br />";
}
else {
die("Couldn't connect" . mssql_get_last_message());
}
if(mssql_select_db($database, $connection)){
echo "Selected $database ok<br />";
}
else {
die('Failed to select DB');
}
the page displays 'couldn't connect' and the error.log says "unable to connect ot server: 192.168.44.96".
I have also set mssql.secure_connection = On in php.ini and restarted apache2.
any thoughts?
thanks!
I can't comment on the PHP part, I only know SQL Server.
But I can tell you: That's not how Windows authentication works.
You can't use Windows authentication and then pass user name and password explicitly to the server. You need to do one of these:
Use SQL Server authentication and pass user name and password explicitly
Use Windows authentication and do not pass user name and password - you will connect automatically with the active Windows user.
Since you're using a Linux server, I'm pretty sure that you can't use Windows authentication. So you have to use SQL authentication (which needs to be enabled on a fresh SQL Server installation), and you have to create an user on the SQL Server.
If you have a default sqlexpress installation you probably need to configure your sql server to allow remote access connections.
Also I don't know if you can connect with windows auth. At your sql server you can also setup sql authentication and use uid=;pwd=.

Categories