Question

I need to config Laravel 4 to use the ODBC PDO Driver for SQL Server 2000

I have tested it in a plain PHP file and it works perfectly, However, I can't get the right config inside Laravel.

This is my connection string >

$conn = new PDO("odbc:Driver={SQL Server};Server=MyHOST;Database=myDb;User Id=sa;Password=;");

I got this so far in the Laravel config/database.php

Edit

Ok, i followed the instructions from https://github.com/ccovey/odbc-driver and i configured:

I changed it to

'odbc' => array(
        'driver' => 'odbc',
        'dsn' => 'Driver={SQL Server};Server=MyServer',
        'grammar' => 'SqlServerGrammar',
        'username' => 'user',
        'password' => 'pass',
        'database' => 'staPPM',
    ),

Im getting an error FatalErrorException, Grammar not Found

Was it helpful?

Solution

Laravel 4 still does not support ODBC directly, you'll have to do it yourself or you can try to use this driver: https://github.com/ccovey/odbc-driver.

You'll have to add connection that should look something like:

'odbc' => array(
   'driver' => 'odbc',
   'dsn' => 'Driver={iSeries Access ODBC Driver};System=my_system_name;',
   'grammar' => 'DB2',
   'username' => 'foo',
   'password' => 'bar',
   'database' => '',
   'grammar' => 'SqlServerGrammar',
),

As noted in the package docs, you have to provide a valid DSN to connect to your server: those ones are examples of valid connection strings:

"Driver={SQL Server};Server=(local);Trusted_Connection=Yes;Database=AdventureWorks;"

"Driver={Microsoft ODBC for Oracle};Server=ORACLE8i7;Persist Security Info=False;Trusted_Connection=Yes"

"Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\bin\Northwind.mdb"

"Driver={Microsoft Excel Driver (*.xls)};DBQ=c:\bin\book1.xls"

"Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=c:\bin"

To know if your DSN is valid, you better test the DNS outside Laravel.

If you have access to a Linux, you can test it by doing:

apt-get install unixodbc

isql -v DSN_NAME db_username db_password

And it should answer with:

+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>

There's a bug in ccovey's source code and, for now, you should alter the source of ODBCDriverConnection to:

/**
 * Default grammar for specified Schema
 * @return Schema\Grammars\Grammar
 */
protected function getDefaultSchemaGrammar()
{
    return $this->withTablePrefix(new \Illuminate\Database\Schema\SqlServerGrammar);
}

I'll open an issue in the package Github so they get this fixed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top