php :Fatal error: Call to undefined function sqlsrv_connect() - php

I Have a big problem And I Wish get Solved for this problem here
I Try to Connect to Sql Server 2008 R2 using:
php 5.6.11
Xampp 5.6.3
Sql Server 2008 r2
Using sql server drivers for PHP (php_pdo_sqlsrv_56_ts.dl &
php_sqlsrv_56_ts.dll)
and my code to try connection is :
$serverName = "Mahmoud-HP\SQL2008R2"; //serverName\instanceName
$connectionInfo = array( "Database"=>"HR16", "UID"=>"Mahmoud", "PWD"=>"123" , "MultipleActiveResultSets" => false);
$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));
}
but when run the page this message come to me
Fatal error: Call to undefined function sqlsrv_connect() in C:\xampp\htdocs\testsqlserver\index.php on
What is solution for this problem

you have to use the SQL Server native driver for php.
Download from here:
https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=20098
Example Code:
$serverName = "tcp:ServerID.database.windows.net, 1433";
$connectionOptions = array("Database" => "DatabaseName",
"UID" => "Username#ServerID",
"PWD" => "password");
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn === false)
{
die(print_r(sqlsrv_errors(), true));
}
EDIT
Make sure you load BOTH dll's listed:
extension=php_sqlsrv_56_ts.dll
and
extension=php_pdo_sqlsrv_56_ts.dll

Related

Connect to MS SQL Server with PHP without drivers

A project at work has come up that requires me to connect to a Microsoft SQL Server 2014 in PHP. I have found tons of solutions online that involve downloading a set of drivers to either use PDO, sqlsrv, or ODBC.
After contacting my hosting provider this is what they said:
As you're on shared hosting, drivers can't be installed as it's not
supported on our platform.
You can use normal ODBC to connect if required
I so far haven't been able to find a solution that works without having to install any drivers/extensions. The hosting provider recommended ODBC but I can't seem to get my code to work for ODBC either. Does anyone know any other way I would be able to do this?
The code I'm currently using (which gives me an error:
Can't open lib 'SQL Server'
$server = 'xxx.xxx.xxx.xxx';
$user = 'user.....';
$pass = 'password...';
$port='1433';
$database = 'database...';
$connection_string = "DRIVER={SQL Server};SERVER=$server;PORT=$port;DATABASE=$database";
$conn = odbc_connect($connection_string,$user,$pass);
if ($conn) {
echo "Connection established.";
} else{
die("Connection could not be established.");
}
Have you tried this?
<?php
$serverName = "serverName\\sqlexpress, 1542"; //serverName\instanceName, portNumber (default is 1433)
$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));
}
?>
Refer: https://www.php.net/manual/en/function.sqlsrv-connect.php

Tring to connect Microsoft Sql server with PHP in MAMP

Im trying to use MS SQL server with PHP in MAMP and I get an error 500.
Below is the php code that Im using.
I learned today that I have to instal a driver but I don't know how to do it in MAMP.:
https://learn.microsoft.com/en-us/iis/application-frameworks/install-and-configure-php-on-iis/install-the-sql-server-driver-for-php
Any idea? many thanks,
<?php
$serverName = "MPR01\SQLEXPRESS"; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"Fund_Lib");
$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));
}
?>
With the error message in hand I just found the answer in this post:
https://es.stackoverflow.com/questions/55679/fatal-error-call-to-undefined-function-sqlsrv-connect

MSSQL DATABASE CONNECTION WITH PHP

Please i am trying to connect to an mssql database on a different machine. Below is my code but i just keep getting a blank page.
I dont know what the issue may be. i have installed php and the mssql drivers.
<?php
$uid = "******";
$pwd = "***\$\$****";
$DB = "***********";
$serverName = "192.***.**.***";
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database"=> $DB, "ReturnDatesAsStrings" => true);
$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));
}
?>
I would suggest that you display the connection error using sqlsrv_errors().
Load the PHP drivers in php.ini file and restart the server.
extension=php_pdo_sqlsrv_54_ts.dll
extension=php_sqlsrv_54_ts.dll
Please see the following example:
<?php
$serverName = "serverName\sqlexpress"; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// 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));
}
?>
For more info: http://php.net/manual/en/function.sqlsrv-connect.php
This is for Server 2019 and PHP 7.4.
Download SQLSRV 5.8.0 Drivers here
Have this in your php.ini: (depending on hardware 32 vs 64)
extension=pdo_sqlsrv_74_ts_x64
extension=sqlsrv_74_ts_x64
Use this to create a mssql connection:
Function connect_db() {
$this->connectionInfo = Array("UID" => DB_USER, "PWD" => DB_PASSWORD, "Database" => DB_NAME);
$this->conn = SQLSRV_CONNECT(DB_SERVER, $this->connectionInfo);
if ($this->conn){ return; }
else {echo "Error";die( print_r( sqlsrv_errors(), true ));}
}

connect to SQL Server Database with PHP

I want to connect to a specific database on SQL Server Through PHP. On WAMP Server, I have PHP 5.6 installed so I downloaded SQLSRV32.EXE (Microsoft Drivers for PHP for SQL Server) then I copied these files in C:\wamp64\bin\php\php5.6.25\ext
IN php.ini, I added
extension=php_sqlsrv_56_nts.dll
extension=php_sqlsrv_56_ts.dll
But when i check phpinfo() on browser i don't see the sqlsrv module listed.
When i try to connect to my SQL Server Database I get this error
Fatal error: Call to undefined function sqlsrv_connect()
Here is my code
<?php
$serverName = "PC0CFEP2\SQLEXPRESS"; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"test");
$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));
}
?>
I was checking my solutions on different forums and I tried many but I still can't get it work. I am sure it's in some settings but i couldn't find out what could be the issue.
Any suggestion please ? Thank you very much.
At First, Please run MSSQL server database.
In php.ini file, Please added below code
extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_56_ts.dll
and keep two dll file \ext folder
To connect database , try
$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));
}
Remove "php_" prefix:
extension=sqlsrv_56_ts.dll
extension=pdo_sqlsrv_56_ts.dll
This worked for me. Thanks for asking this question.

PHP program to connect to SQL Server

I've been doing a project in PHP(xampp) which requires to connect to a SQL Server database.
I've already done things like downloading and installing SQLSRV30.exe in C:\xampp\php\ext folder and creating a simple program which determine whether the program is already connected or not but still couldn't figure it out the problem that i encountered.
$server = "IDEA-PC\SQLEXPRESS";
$dbGet = array("Database"=>"LogboxDB");
$con = sqlsrv_connect($server, $dbGet) or die (sqlsrv_error());
if(!$con)
{
die(print_r(sqlsrv_errors(), true));
}
else
{
echo 'Connected';
}
Here is my error.
Fatal error: Call to undefined function sqlsrv_connect();
How can I get rid of this problem?
You need to enable the extension support to use the MSSQL server in PHP. It can be enabled by editing the php.ini as follow.
extension=php_sqlsrv.dll
You can donwload the package from http://docs.gurock.com/testrail-admin/howto-installing-sqlsrv
Try This
$serverName = "192.168.1.223, 1433"; //serverName\instanceName - change it as yours
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"LogboxDB", "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));
}

Categories