-2

I'm trying to connect on windows my php code with SQL Server(windows authentication). I am using xampp.

This is my code but it's not working.

<?php
          $dbPassword = "";
           $dbUserName = "Inspiron/Malky";
           $dbServer = "localhost";
           $dbName = "person";     

//       $conn = mysqli_connect($dbServer , $dbUserName , $dbPassword,$dbName) or die("unable to connect to host"); 

       $connectionInfo = array( "Database"=>$dbName);
       $conn = sqlsrv_connect( $dbServer, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
?>

ERROR

Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() in C:\xampp\htdocs\connection\connection.php:12 Stack trace: #0 C:\xampp\htdocs\connection\phpAdd.php(2): require() #1 {main} thrown in C:\xampp\htdocs\connection\connection.php on line 12

Could someone help my or give me some links to read?

M Stern
  • 37
  • 9

1 Answers1

0

What the environment? Windows OS Server, Linux (what flavor), MAC OS. If not using MAMP, LAMP, then what web service components are you using?

Post the log file from php that outputs the log/error messages.

Check if sql files exist needed for php such as a directory containing for example;

php_sqlsrv_7_nts.signature
php_sqlsrv_7_nts.so
php_sqlsrv_7_ts.signature
php_sqlsrv_7_ts.so

I found for Linux and for Windows platforms, the SQL drivers need to be installed. You may have to install sql drivers and odbc drivers if you haven't already. Other great documentation located at:

Example application (SQLSRV Driver) https://learn.microsoft.com/en-us/sql/connect/php/example-application-sqlsrv-driver?view=sql-server-2017

How to Install sqlsrv driver on MAMP Mac OS Laravel 2018 https://www.youtube.com/watch?v=PRH04PxZpUk&feature=youtu.be

Ubuntu Linux https://www.microsoft.com/en-us/sql-server/developer-get-started/php/ubuntu/

Download Drivers for PHP for SQL Server https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-2017

ODBC Driver, see example https://github.com/Microsoft/msphpsql/releases/tag/v5.3.0

If using Ubuntu Linux, did you bind mssql to php/appache? For example,

apt-cache search mssql

This will show the database packages

sudo apt-get install freetds-common freetds-bin unixodbc php5-sybase

To test connection to SQL:

tsql -H 10.192.0.10 -p 1433 -U UserAccount -P yourpasswrd -D dbsource

Simple test using php;

<?php
function ConnectDB() {
$conn = mssql_connect(“10.192.0.10”, “UserAccount”, 
“yourpasswrd”);
return $conn;
}
?>
Tiburon
  • 55
  • 1
  • 1
  • 6
  • I'm using windows – M Stern May 08 '19 at 12:16
  • Do you see the sql drivers in the directory for php in the windows machine? for example I show under: .../bin/php/7.1.5/ext php_sqlsrv_7_nts_x64.dll php_sqlsrv_7_nts_x86.dll php_sqlsrv_7_ts_x64.dll php_sqlsrv_71_ts_x86.dll php_sqlsrv_71_nts_x86.dll php_sqlsrv_71_ts_x64.dll php_sqlsrv_71_ts_x86.dll [link]https://github.com/Microsoft/msphpsql – Tiburon May 09 '19 at 14:49
  • i downloaded msodbcsql_17.3.1.1_X64.msi and SQLSRV30. what am i supposed to do now? – M Stern May 10 '19 at 02:53
  • @MStern Edit php.ini - https://learn.microsoft.com/en-us/sql/connect/php/loading-the-php-sql-driver?view=sql-server-2017#loading-the-driver-at-php-startup – Zhorov May 14 '19 at 19:28
  • @Zhorov I added the extension and it's still not working – M Stern May 22 '19 at 21:00
  • @Zhorov This is where i copied my dll files. C:\xampp\php . Is this correct? – M Stern May 22 '19 at 21:14