3

I'm try to connect from php using laravel framewrok to SQL Server, My php version is 5.5.30, PDO drivers installed are: mysql, sqlite, pgsql, odbc. using webServer (MAMP) in OS X El Capitan.

To connect i'm using FreeTDS and unixODBC, both libraries was installed using this articule. https://gist.github.com/Bouke/10454272 and work perfect from terminal.

I'm using the follow code in php file.

$db = new PDO('odbc:Driver=FreeTDS; Server=192.168.1.1; Port=4433; Database=focalyx; UID=myUser; PWD=myPassword;');
}
catch(PDOException $exception)
{
    die("Unable to open database.<br />Error message:<br /><br />$exception.");
}
echo '<h1>Successfully connected!</h1>';
$query = 'SELECT * FROM Users';
try{
    $statement = $db->prepare($query);
    $statement->execute();
}catch(PDOException $exception){
    die("Unable to open database.<br />Error message:<br /><br />$exception.");
}
$result = $statement->fetchAll(PDO::FETCH_NUM);
print_r($result);

however when try to run from localhost, show the this error.

exception 'PDOException' with message 'SQLSTATE[01000] SQLDriverConnect: 0 [unixODBC][Driver Manager]Can't open lib 'FreeTDS' : file not found' in /Applications/MAMP/htdocs/myProject/test.php:5 Stack trace: #0 /Applications/MAMP/htdocs/myProject/test.php(5): PDO->__construct('odbc:Driver=Fre...') #1 {main}.

php is installed in

/Applications/MAMP/bin/php/php5.6.10/conf/php.ini

I don't know if is possible install FreeTDS into MAMP dir or create a kind of symbolic link.

vroldan
  • 1,077
  • 9
  • 12
  • See if these instructions help you: http://stackoverflow.com/questions/16925825/having-troubles-with-unixodbc-freetds-and-pyodbc/17113723#17113723 – Benny Hill Mar 19 '16 at 19:51
  • Thanks for your help @BennyHill, from terminal I can connect well, but the problem is when try to run my website, looks like php doesn’t know where is the FreeTDS driver. – vroldan Mar 19 '16 at 20:06
  • Perhaps: `$pdo = new PDO("dblib:host=mssql;dbname=$dbname", "$dbuser","$dbpwd");` (taken from an old answer of mine here: http://stackoverflow.com/questions/20163776/connect-php-to-mssql-via-pdo-odbc/20165384#20165384 – Benny Hill Mar 19 '16 at 20:35
  • 1
    I took a look again on your first comment and found the error in this file. /opt/local/etc/odbc.ini. Thanks one more time @BennyHill Comments in odbc.ini file helped me a lot. – vroldan Mar 19 '16 at 22:58

0 Answers0