bones
bones

Reputation: 848

Call to undefined function odbc_connect() php 7

I'm testing out php 7 and have come across this error:

Fatal error: Uncaught Error: Call to undefined function odbc_connect()

From the doc page: http://php.net/manual/en/function.odbc-connect.php php 7 is not listed as supported.

Does anyone have a way around this or know when it will be supported?

Thanks@

Upvotes: 14

Views: 93525

Answers (7)

Dung
Dung

Reputation: 20565

Here is the error message:

Redhat PHP Fatal error: Uncaught Error: Call to undefined function odbc_connect()

On Redhat Linux 7 you run:

yum install php-odbc

You will get these packages marked in red:

enter image description here

Code sample to test your connection via php command line run: php [filename].php

<?php
// filename: test-connection.php by running command -> php test-connection.php
$connect = odbc_connect("Driver=FreeTDS; Server=sbase.company.ca; Port=1433; TDS_Version=8; ClientCharset=UTF-8; Database=mydbase",'company\\user', 'password');
$query = "SELECT * from mytable";
// perform the query
$result = odbc_exec($connect, $query);
// fetch the data from the database
while(odbc_fetch_row($result)){
        $suid = odbc_result($result, 1);
        $uid = odbc_result($result, 2);
        $gid = odbc_result($result, 3);
        $name = odbc_result($result, 4);
        print("$name|$suid|$uid|$gid\n");
}
// close the connection
odbc_close($connect);
?>

Enjoy!

Upvotes: 0

user2928048
user2928048

Reputation: 4118

From php.ini file:

> ; Notes for Windows environments :
> ;
> ; - ODBC support is built in, so no dll is needed for it.
> ; - Many DLL files are located in the extensions/ (PHP 4) or ext/ (PHP 5+)

But, in PHP 7, ODBC is not by default. Explicit

extension=odbc

worked for me (new syntax recommended)

Edit: If your architecture is x64 you must use C:\Windows\SysWOW64\odbcad32.exe instead of C:\Windows\system32\odbcad32.exe

Upvotes: 1

Kanna Reddy
Kanna Reddy

Reputation: 325

PHP 7.2.7, add extension=php_odbc.dll in php.ini file while either using database as MS Access or Sql Server C:\xxxxxx\php\php.ini

*no semicolon before to extension=php_odbc.dll

Upvotes: 5

Lenin Oca&#241;a
Lenin Oca&#241;a

Reputation: 81

Just enble "php_odbc.dll" extension by removing the semicolon and restart Apache.

If there is no such line in php.ini, simply create it on yourself (you will find many similar lines in php.ini) by adding: extension=php_odbc.dll and then restart Apache.

If Apache does not start or cannot load php_odbc.dll, look into to the ext-Folder of PHP, if there is such a DLL called php_odbc.dll. If there is no such DLL, Xampp/PHP7 does not support ODBC natively. In that case you should install an older Xampp Version with PHP 5.x

Upvotes: 2

irwinr
irwinr

Reputation: 93

I ran into the same problem. However according to the link you provided PHP7 is in fact supported. So I'm not sure why you have so many comments telling you to go re-write your code.

This is what ultimately fixed the issue for me:

sudo apt-get install php-odbc

Followed by restarting Apache.

Upvotes: 7

Naidim
Naidim

Reputation: 7166

The DOC page does list PHP 7, so just install php-odbc and you should be good to go. Currently using it myself on RedHat EL7 with Remi php7.

Upvotes: 10

Hink
Hink

Reputation: 1103

There is written in doc: ODBC support doesn't need any extension dll. It is true in PHP 5.x, I had to remove "extension=php_odbc.dll" from ini file.
But in PHP 7 I had to put it back.

I found the file "ext/php_odbc.dll" in the new PHP 7 directory again. It works for me :).

Upvotes: 23

Related Questions