Can anyone point me in the right direction for connecting to a SQL Server 2008 database from my website via php?
I would assume its not much different then connecting to a MySQL server.. Any help please, Thanks
I believe this is the script you need...
More on it at http://php.net/manual/en/function.sqlsrv-connect.php
$serverName = "serverName\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$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));
}
Look into the MS SQL Server PDO interface:
http://php.net/manual/en/ref.pdo-dblib.php
I would warn you, it's not a fantastic thing connecting SQL Server 2008 with PHP. I did succeed once, though it was on my local machine running windows. Have a look at the microsoft tutorial on how to connect SQL Server 2008 with PHP here http://msdn.microsoft.com/en-us/library/cc793139%28v=sql.90%29.aspx and the link below will guide you as well. Good luck.
http://hafizbadrie.wordpress.com/2010/02/16/how-to-connect-php-with-sql-server-2008/
Related
Actually I have try to connect SQL server by using sqlsrv_connect and using the DSN (Data Source Name) without Apache service then the both are perfectly working.
The problem is when I turn on the apache service then php cannot connect to the sql server using odbc DSN (Working with sqlsrcv_connect).
The condition is I need to turn on the apache with a application running using sql server which using DSN. I working with crystal report thats why really need this method. I have tried using system DSN instead user dsn, its also not working.
!
I wrote code below to test my scenario
// Connect to the data source
$conn=odbc_connect('DSNNAME','DBUSER','DBPASS');
if ($conn){
echo "Connection established DSN";
}
else {
echo "Connection using DSN Failed:" . odbc_errormsg();
}
// Connect through server name
$serverName = "WEBSERVER\SQLEXPRESS"; //serverName\instanceName, portNumber (default is 1433)
$connectionInfo = array( "Database"=>"DBNAME", "UID"=>"DBUSER", "PWD"=>"DBPASS");
$conn2 = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn2 ) {
echo "Connection established using server name";
}else{
echo "Connection could not be established by using server name";
die( print_r( sqlsrv_errors(), true));
}
The Result when Apache service is on.
Make sure the ODBC DSN matches the Crystal runtime in terms of 32-bit or 64-bit.
I am attempting to get a site using PHP to connect to my Microsoft SQL Server, below is everything I can think of:
Server: 2012
SQL Server: 2012
PHP: 7.2.6
Code:
<?php
$serverName = "ServerName";
$connectionInfo = array( "Database"=>"DBname", "UID"=>"user.name", "PWD"=>"K5zUtwDdzyX8T1vmvtEL");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn==true ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
Other:
The Site and SQL Server are on different machines
Both boxes are Windows
PHP was installed manually not through IIS
Only error I receive is "500 Internal Server Error"
I have installed the drivers from Microsoft
If there is anything I have left out please let me know and I will promptly provide it. I have been lost for quite some time, I am open to any suggestions.
#Magnus Eriksson
Thank You, I enabled the error display using your link which then displayed the error:
Fatal error: Call to undefined function sqlsrv_connect()
Which led me to this post: Fatal error: Call to undefined function sqlsrv_connect() so adding the below to my ini file resolved that issue.
extension = php_sqlsrv_72_nts_x64.dll
I'm attempting to connect to a MS SQL Server via PHP7 sqlsrv driver.
It seems like when my browser tries to execute:
$conn = new PDO( "sqlsrv:Server=$serverName;Database=mydb", $uname, $pass);
or
$conn = sqlsrv_connect( $serverName, $connectionInfo);
it just hangs. No success or failure echo/die messages are received. Instead, the wheel in my browser keeps spinning and the status message is "waiting for localhost". It also doesn't seem to ever timeout. I've waited 10+ minutes and the connection never fails/succeeds.
Full code (I've tried procedural and PDO methods):
<?php
//
$serverName = "myserver\myinstance"; //serverName\instanceName
$uname = "myuser";
$pass = "mypass";
$connectionInfo = array( "Database"=>"mydb", "UID"=>$uname, "PWD"=>$pass);
/*Procedural sqlsrv method
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
die("DONE");
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
*/
/*PDO sqlsrv method*/
try {
$conn = new PDO( "sqlsrv:Server=$serverName;Database=mydb", $uname, $pass);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
die("success!");
} catch(PDOException $e) {
die($e->getMessage());
}
?>
Setup details and other information:
I'm using XAMPP.
Windows 7 x64
PHP 7.0.9
I'm trying to connect to an instance of Microsoft SQL Server version 10.50.6220.0 on a Windows NT machine
I need to authorize via SQL Server Authentication -- not Windows Authorization
I'm able to successfully connect to the database using Microsoft SQL Server Management Studio and other tools (FME Workbench)
The server is remote, but is accepting remote connections (as verified with SSMS)
I previously had issues with "undefined function sqlsrv_connect()" until I installed
php_sqlsrv_7_ts_x86.dll
and
php_pdo_sqlsrv_7_ts_x86.dll
into my php/ext folder and updated the php.ini. When I open phpinfo I see sqlsrv in both the "Registered PHP Streams" and "PDO drivers" fields.
What should I do to get a successful connection or get more information about why my connection attempt does not succeed/fail?
EDIT:
I've switched to XAMPP (PHP 5.6.24). I am able to connect to the SQL Server via CLI now, but NOT able to connect via PHP-CGI (through the browser/served by Apache HTTPD). I've verified again through php_info() that the sqlsrv and PDO extensions have been successfully loaded. I'm going through the differences between PHP-CGI and PHP CLI now to see what might be causing the issue.
EDIT:
I'm using IIS now instead of Apache and am able to connect via sqlsrv without issues. Not really a solution, but a way around the problem.
try like that :
try {
$db = new PDO( 'sqlsrv:server=serwer_name;Database=database_name', 'user', 'password');
} catch (PDOException $e) {
echo $e->getMessage();
}
I am trying to connect to a SQL server database with a windows account. In sql managment studio, I can open the database with this account but not with my php application. This user is not a sql user, but he have the right on the SQL database.
I tried with a sql user and it works with my php application.
Is it possible to use my windows account from the php application, and not a specifique sql user?
sqlsrv_connect
By Default it trys to establish a Connection with a Windows Auth. u can try the snipped below. If u have to use a defined WindowsUser to Login, try the Code from the documentation link above.
$serverName = "serverName\sqlexpress"; //serverName\instanceName
// 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));
}
There is a part of code with connection string in which you provide instance name, login, password etc for PHP to connect to MS SQL. You can change connection string on your credentials from windows domain. But its acceptable for testing but not for production :)
Another way is to connect PHP to your AD and make user to enter login/pass from domain when they come on your site. For example it is simply done in IIS (I haven't work with domain auth on PHP that runs on Linux, but as I know LDAP can help you).
I'm trying to connect to a remote ms sql db (not a localhost), but everytime it timeouts before it sucseeds...
I'm pretty sure that the problem is the $serverName variable, is there anyway to check via Plesk Parallels what is the value of that?
<?php
$serverName = "server's ip address/database name"; //serverName\instanceName
$connectionInfo = array( "Database"=>"database name", "UID"=>"DBusername", "PWD"=>"DBpassword");
$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));
}
?>
Try to inject precise port number into ServerName like
<?php
$serverName=127.0.0.1\SQLEXPRESS,1433; //networkAddr\InstanceName,<portNum>
?>
Try using Ip address instead NetBios name if you do, cause ICMP protocol is usually blocked due security policy.
Also, the default port 1433 may be blocked. Should contact server administrator to provide actual connection requisits in that case.
The problem was that my ip address was not permitted to connect to the database, after I contancted my hosting service provider they gave me that permission and it succeeded.