Connect php application to MS SQL server - php

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

Related

PHP Connection statement for SQLserver

I have a SQL server on MS azure. Using a php file and some basic select statements (eg. select * from table) I have been able to grab data from the database, so I know my details are correct.
However when I put a connect statement in any other files the page appears blank. For example, any form submission pages.
This is the connect statement I have:
<?php
$myServer = "hostname";
$myUser = "username";
$myPass = "password";
$myDB = "dbname";
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
?>
I'm sure it's probably something simple but I have tried loads of different ways and nothing seems to work, I don't even get an error message to work with.
Could someone please help?
Uncomment this line in your php.ini.
extension=php_mssql.dll
To locate your php.ini file, add this code below at the top of your php script
<?php phpinfo(); ?>
By default, Azure environment doesn't install php_mssql.dll extension, it installs php_sqlsrv.dll instead.
You can leverage Console tool in Azure portal and execute php -m to check all the preinstalled modules in Azure PHP runtime:
You can use PDO in PHP to handle sql server database, such as the following connection test:
try {
$conn = new PDO("sqlsrv:server = tcp:garysql.database.windows.net,1433; Database = garymbdata", "gary", "QWEasdzxc123");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
print("Error connecting to SQL Server.");
die(print_r($e));
}
Additionally, we can configure PHP configuration settings on Azure Web App beside modifying php.ini directly. Also we can install mssql.dll extension on Azure Web App if you insist on.
Please refer to https://learn.microsoft.com/en-us/azure/app-service-web/web-sites-php-configure for more info.

DB2 connection failed php

I'm trying to connect to remote DB2 via PHP. But have some problems. I've already installed IBM Application developer client.
phpinfo() output:
IBM DB2, Cloudscape and Apache Derby support enabled
Module release 1.9.4
Module revision $Revision: 327944 $
Binary data mode (ibm_db2.binmode) DB2_BINARY
Then, I've got a php file which is looking like:
$database = 'MyDB';
$user = 'db2inst1';
$password = 'mypassword';
$hostname = '1.1.1.1';
$port = 50000;
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;PORT=$port;HOSTNAME=$hostname;".
"PROTOCOL=TCPIP;UID=$user;PWD=$password;";
$conn = db2_connect($conn_string, '', '');
if ($conn) {
echo "connection to $database succeeded";
} else {
echo "connection to $database failed";
echo db2_conn_errormsg();
}
And trying to execute this file, I have "connection to MyDB failed", and NO visible response from db2_conn_errormsg(), which is actually making me baffled
Unfortunately, I haven't got a straight access to the remote server with database. But several months ago, when I was using other client, I succeeded to connect to exactly this database. But that time I didn't need to install IBM ADCL. That is why I can guess that problem is on this side. But even if so, I couldn't fix it.
Sorry if I duplicated some question on stackoverflow, but all answers, which I found, were unfortunately useless to me.
I'm using Apache 2.2 and PHP 5.4.
Hope you can help.
Thanks for any replies!
Are you sure you have connectivity to the server? Correct port, server, firewall rules, username, password, database name?
What is the SQL code you are receiving. Try to get the SQL code from PHP, "connection to xx failed" is your own code so it is useless to help you.
Did you install the application development client? which DB2 version are you using? ADCL is old, it was for DB2 8. Since DB2 9.7, clients have different names, and I think you need IBM Data server client in order to compile the php module. For more information, check this website: http://www-01.ibm.com/support/docview.wss?uid=swg27016878
I think you have to catalog the database server (node) and the database in the local machine with the db2 client. It seems that your PHP code uses ODBC driver, and it has to be configured locally.
Your connection string looks like an ODBC connection where as the db2_connect function in PHP needs :-
DATABASE=database;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=username;PWD=password;
It's on the PHP web page.
I have never been able to get ibm_db2 or pdo_ibm working on a remote. They work if DB2 and Apache are on the same machine (like the iSeries) but not if the host is connecting to a remote DB2.
If you read the Doctrine2 PHP drivers for each you will find they redirect to ODBC if the host is not 'localhost' or '127.0.0.1'.
This code works for me
<?php
if (!$db = odbc_connect ( "AS400", $user, $password) )
echo 'error!';
$result = odbc_exec($db, "select count(*) from $table");
while (odbc_fetch_row($result)) {
var_dump($result);
print_r($result);
echo "\n";
echo odbc_result($result, 1)."\n";
}
odbc_close($db);
AS400 is DSN name defined in ODBC.

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

PDO and SQL Server using SQL Server Auth instead of Windows Auth

My environment:
- Windows Server 2008 R2.
- Apache Webserver.
- PHP 5.4.7
I've just downloaded and installed Microsoft Drivers 3.0 for PHP for SQL Server.
I've included "php_pdo_sqlsrv_54_ts.dll" into php.
Microsoft has included a helpful chm file with the drivers (SQLSRV_Help.chm).
In this help file there is a PDO example using windows authentication:
/* Connect using Windows Authentication. */
try {
$conn = new PDO("sqlsrv:server=$serverName ; Database=AdventureWorks", "", "");
$conn->setAttribute( PDO::ATTR ERRMOE, PDO::ERRMODE EXCEPTION );
}
But I have to connect to our MS SQL Server 2008 R2 using SQL Server Authentication!
What is the difference is between connecting to SQL Server
using PDO and Windows Authentication
using PDO and SQL Server Authentication
Is it just the connect string? Do I have to write:
$conn = new PDO("sqlsrv:server=$serverName; Database=AdventureWorks; UID=sa; PWD=abc");
or do I have to write:
$conn = new PDO("sqlsrv:server=$serverName; Database=AdventureWorks; User Id=sa; password=abc");
Thanks in advance
Update:
I found a resoure:
http://www.connectionstrings.com/sql-server-2008
There are optional second and third parameter in the constructor which specify the username and password as described here:
PDO::__construct
Creates a connection to a SQL Server database.
Syntax PDO::__construct($dsn [,$username [,$password
[,$driver_options ]]] )
Parameters
$dsn: A string that contains the prefix name (always sqlsrv), a colon,
and the Server keyword. For example "sqlsrv:server=(local)". You can
optionally specify other connection keywords. See Connection Options
for a description of the Server keyword and the other connection
keywords. The entire $dsn is in quotation marks, so each connection
keyword should not be individually quoted.
$username: Optional. A string that contains the user's name. To
connect using SQL Server Authentication, specify the login ID. To
connect using Windows Authentication, specify "".
$password: Optional. A string that contains the user's password. To
connect using SQL Server Authentication, specify the password. To
connect using Windows Authentication, specify "".
[...]
This implies that you can connect to SQL Server using PDO + SQL authentication like this:
$conn = new PDO(
"sqlsrv:server=$serverName ; Database=AdventureWorks",
"sa",
"strong password"
);
The connection options section also mentions that you cannot use UID and PWD parameters in connection strings when using PDO_SQLSRV.

Connecting From PHP Server to MySQL Server on Amazon AWS

I have two servers setup on Amazon AWS. One server is running PHP and the other has MySQL. I can connect to the MySQL database through my terminal and MySQL Query Browser.
I am trying to get a connection between the PHP and MySQL servers.
Here is the PHP Code that I am using (works for "localhost" databases)
$dbbase = 'mydb';
$dbuser = 'myuser'; // Your MySQL username
$dbpass = 'mypassword'; // ...and password
$dbhost = 'localhost:3306'; // Internal IP for MYSQL Server
// connect to database
$dblink = mysql_connect($dbhost, $dbuser, $dbpass)
or die ("System Down");
mysql_select_db($dbbase, $dblink)
or die ("Database Down");
It is my understanding that I should be able to route this an internal AWS traffic, but at this point I will take anything that works and build from there.
Here is what I have done:
Added the ip of the PHP server to the Security Group for MySQL(3306) permissions
Tried to use the internal, external, and private IPs/DNSs of the MySQL Server as the $dbhost variable
Created myuser#% (wildcard) on thy MySQL server
Any ideas or tips would be much appreciated.
I had the same issue - turns out MySQL extension for PHP is NO longer included in PHP5!
"In PHP 5 (updated PHP 5.0.4), the following changes exist. Built in: DOM, LibXML, Iconv, SimpleXML, SPL and SQLite. And the following are no longer built in: MySQL and Overload."
Source: http://php.net/manual/en/install.windows.extensions.php
I've got this working.
I think the big trick was to add this rule to the Security Group:
Type: MySQL
Source: 10.0.0.0/8
My understanding is that 10.0.0.0/8 covers all internal amazon IPs. I think you could tighten this up, but it is possible for the internal IP of your servers to change, so that would need to be managed.
Then in my PHP script I used the Private DNS of my MySQL Server. It should look something like this: ip-10-10-100-100.ec2.internal:3306
In the end, I think that is everything that I did.

Categories