ODBC Connection with integrated security PHP and MS SQL Server - php

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

Related

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

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

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

Connect php application to MS SQL server

TLDR: I'd like to connect a php application to a MS SQL server through an active directory account but haven't had much luck finding documentation on how to do that.
Most of the issues that people seem to have with this are pretty complicated I think I just don't have a strong enough grasp of the concept. When I try to connect it to the server, I get:
Warning: mysql_connect(): No connection could be made because the target machine actively refused it.
The code I'm using is:
$user_name = "ADAccountName";
$pass_word = "ADAccountPW";
$datbase = "myDatabase";
$server = "#ipaddress#:#port#";
$db_handle = mysql_connect($server, $user_name, $pass_word);
$db_found = mysql_select_db($database, $db_handle);
I created a user "sa" through MS SQL Server Management Studio and attached it to an AD account (I don't seem to have access to create one without picking an AD account).
I've had luck connecting it to a database using MySQL Workbench (for testing) and a dedicated username/password, but the actual database I need to use is run by MS SQL Server Manager with an AD account. I've spent the greater part of the day going through forums and Microsoft's documentation on SQL Server Manager but I haven't found anything that can help me out with this.
I'm using php v. 5.2
Depending which version of PHP you are using you could do the following
resource mssql_connect ([ string $servername [, string $username [, string $password [, bool $new_link = false ]]]] )
URL - http://php.net/manual/en/function.mssql-connect.php
This would give you direct connection to the DB.
If you're trying to connect to MSSQL you will need to do a few things to your web server first.
1.You will need to add PHP to your windows startup
2.Then you will need to install SQL Server Native client
3.Install the SQL Server drivers for your web server. SQLSRV30 is for PHP 5.4, SQLSRV31 is for PHP 5.5
4.Then go to your php.ini file and enable these extensions.
This will enable you to use PHP Data Objects.
You can connect to the DB like this:
try
{
$conn = new PDO( "sqlsrv:server=$serverName ; Database=AdventureWorks", "sa", "");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(Exception $e)
{
die( print_r( $e->getMessage() ) );
}

Connecting to an MSSQL database through ODBC Error

Where to start..
I have a server with windows 2008 R2 standard 32 bits.
On that server ISS 7 is installed and a php site is added but requires databases.
Made an sql database on that server wich is called 'test'.
ODBC connection is made and tested and it succeeded.
Now when I use this php code to connect to the server it gives me:
Can not connect because the target machine actively refused it.
php code:
<?
### Database ###
$user = "*****"; //database user
$pass = "*****"; //database password
$host = "localhost"; //database location
$db = "test"; //database name
##mysql_connect ("$host","$user","$pass");
##mysql_select_db("$db");
$dbConn = odbc_connect("$db","$user","$pass","$host") or die(odbc_errormsg());
?>
The password and username are correct but the error still remains.
Any help is appreciated!
Have you set your datasource using administrative tools, data sources, System DSN?
if not i'm sure it won't find the database.
see this link: http://support.microsoft.com/kb/305599

Creating a Secure Connection to a Server using PHP

I am trying to query a database that's on a server that I have a Remote Desktop connection to. What is required to be able to query a database on this server using my php web application.
You need:
Database server application configured to allow connections from external hosts
Server-side network configuration tuned to allow connections from external hosts (firewall, NAT, ...)
Database user, which is granted access to database you are going to use
PHP application, which connects to your database server under appropriate user
Details depend on what database server are you using.
$link = mysql_connect($host, $username, $password);
There is nothing secure at all by the way.
I use something like this, but it's far from secure. I keep it in a seperate "connection.php" that are required once by other files.
$db_host = 'INSERT IP HERE';
$db_user = 'User';
//My sql password for testing purposes
$db_pass = 'PASSWORD';
//name of the database
$db_name = 'dbname';
//connection
$conn = new mysqli($db_host, $db_user, $db_pass ,$db_name);
if (mysqli_connect_errno())
{
printf("Error connecting to DB, Errorcode: %s\n", mysqli_connect_error());
exit;
}
use answer by Merch member - the php code above .
You need to grant privileges and to create user on server you are retrieving the database data from.
Connect to mysql :
in shell type (if you are root type root or whatever username):
mysql -u root -p
enter password,
when you are logged in :
create user someusername#your-client-ip identified by 'password';
grant all privileges on *.* to someusername#your-client-ip with grant option;
now you can use php code above to connect to remote server database (ip you just used to create mysql user your-client-ip)
When you are creating php variable for mysql connect - host use mysql port in the variable if just ip not connects your-client-ip:3306
good luck

Categories