Description of the problem
Error message:
Fatal error: Uncaught Error: Call to undefined function odbc_connect()
in ...
Meaning:
The odbc_connect
function does not exist because the required extension is not loaded.
PHP / ODBC installation on Linux
Depending on the Linux distribution used, it will be necessary to use the specific package manager of each one to install the necessary one.
In the case of distributions based on RedHat (such as CentOS, Oracle Linux, etc), Debian (Ubuntu, Xubuntu, Guadalinex, etc.) and SUSE (OpenSUSE) the package is called php-odbc
and the command needed to perform the installation (as root
) varies depending on the distribution:
apt-get install php-odbc # Debian Stretch / Ubuntu 16.04 LTS / etc
yum install php-odbc # Oracle Linux 7 / Fedora 27 / etc
zypper install php5-odbc # OpenSUSE 42.3
PHP / ODBC installation in Windows
Depending on the package or distribution downloaded (such as XAMPP or WAMP ) everything necessary is included but is not activated by default.
To activate the PHP / ODBC extensions you must add the following line in the file php.ini
:
extension=php_odbc.dll
After doing so, you must restart the apache server for the changes to take effect.
Name of the ODBC drivers available in Windows
To obtain a list of available drivers installed in Windows, you must inspect the following registry key:
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers
To obtain a list from the command line, the following instruction can be executed:
reg query "HKLM\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers"
Whose sample output would be:
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers
SQL Server REG_SZ Installed
SQL Native Client REG_SZ Installed
IBM DB2 ODBC DRIVER - DB2COPY1 REG_SZ Installed
IBM DB2 ODBC DRIVER REG_SZ Installed
BMC ODBC for PostgreSQL ANSI (Default) REG_SZ Installed
BMC ODBC for PostgreSQL UNICODE (Default) REG_SZ Installed
BMC ODBC for Oracle (Default) REG_SZ Installed
So your connection string should be:
$conexión = odbc_connect(
"Driver={SQL Server};Server=$server;Database=$database;",
$user,
$password
);
You can also view the available and the driver version through "ODBC Data Sources (32/64 bits)" in the "Drivers" tab: