PHP program to connect to SQL Server - php

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

Related

php Call to undefined function sqlsrv_connect()

I have windows 10 64 bit and installed xampp version 7.3 and I am trying to connect with SQL server using my below code but it always gives me that error:
"Uncaught Error: Call to undefined function sqlsrv_connect()".
I have downloaded ODBC versions 11,13,17 and I downloaded dll drivers and located them in my "xampp/php/ext". also I have changed the extension in my php.ini file and tried every single extension with different versions but nothing works!
<?php
echo phpinfo();
$serverName = "ServerNAme"; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"DB[![enter image description here][1]][1]");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
sqlsrv_close($conn);
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>

Xampp not connecting to SQL Server

I have loaded the SQL drivers in php/etc folder and also have edited path in php.ini. I can see sqlsrv in php info as well.
This is the code that I am using to connect to SQL Server on another server.
I tried server name "10.10.10.38:1433" this also nothing worked for me.
$serverName = "10.10.10.38,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));
}
But I am getting following error:
Please help to resolve the issue.
Have you enabled pdo_sqlsrv ?
Have you installed ODBC drivers?
https://learn.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-ver15
After installing the right ODBC drivers as Maxi Malvido told you you can use a PDO connection:
<?php
$conn = new PDO('sqlsrv:Server=yourserver; Database=test', 'login', 'password');
if ($conn)
{
echo "Connection established";
}
else {
echo "Connection not established";
}
?>

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

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.

Categories