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
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'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 use Microsoft server in a PHP program. I downloaded the Microsift drivers for PHP for SQL Server, made sure that Microsoft SQL Server 2012 Client was installed, made sure the extension_dir value was correct in my PHP.ini file, and added extension=php_sqlsrv_53_ts.dll in the dyanmic extension section of my PHP.ini file. Yet i still get the error 'Fatal error: Call to undefined function sqlsrv_connect()'. I am running an apache server on windows 7 and downloaded the 3.0 version of the driver.
So what am I doing wrong? What else should I check for? If I am running under an Apache server, I don't need to do anything to IIS, do I? Below is my PHP code.
<?php
// phpinfo(INFO_MODULES);
require_once 'serverlogin2.php';
/* Specify the server and connection string attributes. */
//$connectionInfo = array ("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd);
$connectionInfo = array ("Database"=>$databaseName);
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Unable to connect.</br>";
die( print_r( sqlsrv_errors(), true));
}
echo "made sql connection";
return;
?>
sqlsrv_connect (sqlsrv extension) is not available in the GA branch, you need to get it from svn and compile it. You can also use the mssql_connect (mssql extension) or PDO.
I'm trying to connect a php script to a Microsoft SQL Server, Additionally I'm doing some testing on my own to learn more about php and servers.
if(function_exists("sqlsrv_connect"))
{
echo "exists<br/>";
}else
{
die("does not exist<br/>");
}
$serverName = "myTestServer";
$connectionInfo = array( "Database"=>"test_name", "UID"=>"testUser", "PWD"=>"testPwd");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if($conn)
{
echo "sucessful";
}
else
{
echo "failed ";
}
The output is:
exists
failed
Therefore I can see that the .dll was installed as intended. Am I missing something else? or could this be a problem in the server properties?
Whenever a failure occurs with a SQLSRV PHP function, call sqlsrv_errors to find out the root cause. In your case, it seems you don't have the correct SQL Server ODBC drivers installed on the PHP server. Install the drivers as recommended by your error message, and things should improve.
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/