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.
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 have searched through many posts trying to find an answer but get nowhere.
I am trying to establish a connection to and Sql Server DB through my PHP web application using WAMP.
What I have tried:
I downloaded the sql drivers for PHP 7 and 7.1 and tried them with the corresponding PHP versions
I made sure to restart all services after updating the php.ini file.
I haveinstalled the SQLSRV40.EXE and updated the php.ini with:
extension=php_pdo_sqlsrv_7_ts_x64.dll
extension=php_pdo_sqlsrv_7_nts_x64.dll
I did not though that even though these are added in the php.ini they were not in the php> php extentions list - not sure why
This is my code below allow with the error
<?php
$serverName="DESKTOP-0KNJ0KP";
$connectionInfo=array("Database"=>"SPMS_db",);
$conn=sqlsrv_connect($serverName,$connectionInfo);
if ($conn) {
echo "Connected.<br />";
} else {
echo "Connection failed.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
I have added context fro PHPinfo()
You have installed PDO_sqlsrv part of PHP Driver for SQL Server, but your code uses sqlsrv functions. You have two options:
install php_sqlsrv_ extensions to make these functions work or
rewrite your code to use PDO version of the driver
PHP code using PDO version of PHP Driver for SQL Server:
<?php
# Connection
$server = "DESKTOP-0KNJ0KP";
$database = "SPMS_db";
try {
$conn = new PDO( "sqlsrv:Server=$server;Database=$database", NULL, NULL);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch( PDOException $e ) {
die( "Error connecting to SQL Server. ".$e->getMessage() );
}
# End
$conn = null;
?>
Connecting through PDO library (which is already installed in your server) is easier.
See PDO Book from PHP.NET: https://www.php.net/manual/en/ref.pdo-dblib.php
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
So I've re-installed my "webserver". It had some OS issues and I decided it was time ! I copied/backed-up the entire WAMP folder and restored it nicely after reinstall. All of that works, just not the site like it should. In essence, the sqlsrv_connect seems not to connect ! It doesn't give me any error output. $conn = sqlsrv_connect($serverName, $connectionInfo); will just come out "false"
My setup :
'''
Windows 7 Enterprise 64 bit SP1
Wamp Version 2.5 (32bit)
Apache Version 2.4.9
PHP Version 5.5.12
MySQL Version 5.6.17
Microsoft SQL Server 2005 (on an external server)
extension=php_pdo_sqlsrv_55_ts.dll
extension=php_sqlsrv_55_ts.dll
'''
the PHP runs just fine in another server. That server however uses IIS.
$serverName = "first.second.third.fourth.fifth";
$connectionInfo = array("Database"=>"Databasename", "UID"=>"Username", "PWD"=>"password");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if (!$conn) {echo "no connection to database/server";}
$sql = "SELECT * FROM Tablename";
$results = sqlsrv_query( $conn, $sql ); if($results === false) {print_r( sqlsrv_errors(), true);}
What I've tried so far :
checked that the server actually can be accessed. Same code works
fine on the IIS server.
tried to restore backups from previous PHP files I had.
checked the PHP.INI file(s) and checked that the sqlsvr extensions
are activated.
tried the same site on another wamp webserver.
Disabled AV to test if it's not maybe the connection/firewall to the SQL server.
checked that admin priv's are on the essential wamp exe's
What am I missing ? I truly think it's a small setting somewhere - just can't figure out where or what ! Maybe it's a setting that allows this system to connect to the SQL server ? This setting is enabled on the IIS server by default... ? Maybe I need to pass additional authentication somehow ?
Firstly, I used this to get the error message :
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
Then the issue was it needed the ODBC Driver which I got from here:
https://www.microsoft.com/en-us/download/details.aspx?id=36434
Downloaded the 64 bit version and installed it and all was working again.
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.