4

I am trying to create an app fir the first time using PDO and when i

try {
    $db = new PDO('mysql:host=localhost;dbname=DBname', 'user', 'pass');
} catch(PDOException $e){
    echo '<div class="errorMSG">Failed to connect to database. Please try to refresh this page in 1 minute. However, if you continue to see this message please contact your system administrator.</div>';
    echo '<br /><br />' . $e->getMessage();
    exit();

}

ofcourse i have change pass user and DBname to the correct once but i have get an error could not find driver

So with few searches i found out that i need to enable pdo_mysql but i can't seems to find out where do I do that and how? Can some one please help me with how to enable this and/or get this to work on my local machine. I have windows 7. I can't find where is my php.int is located.

Thanks :)

Jaylen
  • 39,043
  • 40
  • 128
  • 221

1 Answers1

6

Take a look into the PDO installation section of the PHP manual.

It depends on the OS of your machine. If you compile php yourself, and you wish to install the MySQL PDO driver, then you should add the --with-pdo-mysql config option before compiling PHP.

On a windows machine, just enable those two extensions...

extension=php_pdo.dll <- only if below php 5.3
extension=php_pdo_mysql.dll

...in your php.ini and restart your app server. The php.ini should usually be located in your php directory. Where it is located depends on where you've installed php.

Bjoern
  • 15,934
  • 4
  • 43
  • 48
  • Thanks for your help. I am closer to the truth i guess but still problem is not solved. php.int never existed and it was php.int-recomended so i renamed it to php.int and i removed the semi-colen that was before both of the lines you stated. I have restarted my appache and it did not work. it stiill give me "could not find driver" the other option is (--with-pod-mysql) where do I add this and how? My OS is Windows 7 thanks – Jaylen Mar 05 '13 at 19:55
  • Forget about `--with-pdo-mysql` on windows. If you work on windows, you just have to enable the two extensions stated in my answer in your `php.ini`. Try a `phpinfo();` to see which drivers are currently enabled, there should be a PDO section. – Bjoern Mar 05 '13 at 20:07
  • Okay, I am looking at my phpinfo and here is what I see PDO, PDO Drivers enabled are sqlite, sqlite2, I am runing PHP 5.2.6 as of now. so i enabled both of these extension=php_pdo.dll extension=php_pdo_mysql.dll. – Jaylen Mar 05 '13 at 20:12
  • It _should_ work if both extensions are enabled. Check your apache error log if it encounters errors while restarting your apache, maybe a DLL isn't located where it should be. – Bjoern Mar 05 '13 at 20:25
  • Looking at the log then only thing that failed twise today is PHP Warning: PHP Startup: Unable to load dynamic library 'C:/AppServ\\php5\\ext\\php_exif.dll' - The specified module could not be found.\r\n in Unknown on line 0 I don't know if this is the problem or part of it? – Jaylen Mar 05 '13 at 20:29
  • `exif` is needed for working with images. Disable this extension if you don't need it and see, if the error behaviour changes. – Bjoern Mar 05 '13 at 20:59
  • That did not work. I actually found the file and placed it there. But i am still having the same issue. so I installed a newer version of AppServ which has php 6.0 so this should have solved the probolem but unfortunatly the problem still exists! – Jaylen Mar 05 '13 at 21:39
  • 1
    Change your `php ini` and see if this has any effect on `phpinfo();`. My guess is your php installation doesn't read the `php.ini` you're editing. Maybe check `c:\windows\` if there is another `php.ini`, then work with this one. – Bjoern Mar 06 '13 at 05:16
  • 1
    It finally worked after i installed the newer version and added extension=php_pdo_mysql.dll in to my php.ini file because it never existed there. Thanks – Jaylen Mar 06 '13 at 19:08