简体   繁体   中英

Laravel 4 on Mac localhost: Cannot find driver for SQLSRV

I'm trying to connect with Laravel 4 to an SQL Server database. I'm getting an error, but I think it's a PHP error, not a Laravel error. It says PDOException: could not find driver . I have the driver set to sqlsrv like another example in the database.php file. Like I said, I think this is a PHP error, not Laravel.

I'm on localhost on my Mac. Do I need to configure something in my php.ini file? I found another article that said I need to comment out a MySQL driver under PDO. I also couldn't find that line in my php.ini . But I need to connect to a MySQL database as well, anyway. What do I need to change in my php.ini file?

I found this line:

pdo_mysql.cache_size = 2000

But commenting it out did nothing even after restarting Apache.

The sqlsrv driver is a windows-only driver according to the PHP Manual (www.php.net/manual/en/sqlsrv.requirements.php). So you can't use that driver on Mac, Linux, Unix or any other operating system than the ones listed on that page.

I assume you are trying to connect to a remote SQL Server somewhere? If you're on Mac, then SQL Server wouldn't be running on your local machine, so I guess the first question is whether or not you have a SQL Server running somewhere.

Second, from the server that is running PHP, you would need to connect using either FreeTDS or ODBC with FreeTDS.

Since Laravel 4 uses PDO, you'll need to install FreeTDS and ODBC. Laravel 4 doesn't use the mssql PHP extension. It uses PDO, so mssql won't get you anywhere with Laravel 4.

If you're serving PHP from your Mac, I would say there are generally 4 different ways you could go:

  1. The Easy, but costly way: Go to (www.macupdate.com/app/mac/23152/openlink-odbc-driver-for-sybase) and download and install OpenLink ODBC driver. This allows you to create new ODBC connections using the Sybase (MSSQL) driver on a XNIX machine. There's an installation walk-through here (wikis.openlinksw.com/dataspace/owiki/wiki/UdaWikiWeb/InstallSybaseExpressOSX). It comes with a 15 day trial, after which you would have to purchase it. As of this writing, it appears to cost somewhere in the range of $30 - $50 depending on the license you choose.

  2. The harder, but free way: Follow http://blog.benjaminwalters.net/?p=10 (slightly outdated) to re-compile FreeTDS support into PHP on Mac (assuming you're using MAMP)

  3. The far better, but also far more work way: Find a Virtual Machine Host software like VMWare Fusion and install a Linux server like Ubuntu 12.4. Make sure you install apache2 and PHP during the install process and then follow the steps here to get FreeTDS working on your new server machine.

  4. Run PHP from the Windows Server that is running SQL Server

Milage will vary with each solution, and there is a learning curve in any case. I've been a PHP developer for a long time, and it took me days to figure out how to get connected to SQL Server for the first time from Linux. Thankfully, there are quite a few walkthroughs out there now, and FreeTDS has come a long way since that time.

Hope that helps. :)

I actually have this running on my setup. My best advice is to get on the Homebrew bus.

https://github.com/josegonzalez/homebrew-php is where you're going to want to look for installing PHP and related items. I don't remember exactly what I did but it wasn't particularly difficult.

First you'll need FreeTDS.

brew install freetds

Then, add Jose's formulas to your homebrew (instructions on the github page). After that,

brew install php5x --with-mssql  

brew options php5x

if you need more extensions.

Based on Jon Watson's comment,

I've tried step 2, and it doesn't work. check my issue here https://github.com/laravel/laravel/issues/2180

Step 3 is broken link and I think it's the same with step 2.

I also tried linux ODBC driver for Laravel (there are 2, google it), both doesn't work too . Although my simple test php works in both DBLIB and ODBC (compiled using official microsoft odbc unix driver).

Step 4 definitely works, BUT you must use IIS for best stability.

That leaves step 1. OR,

Ditch Laravel4 and use good ol' AdoDB http://adodb.sourceforge.net/ which I've been using since 2006 and still working superbly (although not modern like Laravel) configure it using tutorial in http://docs.moodle.org/20/en/Installing_MSSQL_for_PHP

I wonder why they don't build sqlsrv driver for linux too....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM