3

am using following code to connect to database

//database credentials
define('DBHOST', 'localhost');
define('DBUSER', 'sanoj');
define('DBPASS', '123456');
define('DBNAME', 'test');

//application address
define('DIR', 'http://www.blogtom.com/');
define('SITEEMAIL', 'sanoj26692@gmail.com');

try {

    //create PDO connection 
    $db = new PDO("mysql:host=" . DBHOST . ";port=3306;dbname=" . DBNAME, DBUSER, DBPASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $db->exec("SET NAMES 'utf8';");
} catch (PDOException $e) {
    //show error
    echo '<p class="bg-danger">' . $e->getMessage() . '</p>';
    exit;

after i run my php file from netbeans ide i get nothing except this

could not find driver

i tried all method checking php.ini file

and even read this https://stackoverflow.com/a/2852997/3836908 tofind some solution but cant find whats wrong

i used this code in PC it worked well but when i try this on my laptop i get this error how can i solve this problem

am using windows 64bit but previouly i used 32bit

sanoj lawrence
  • 951
  • 5
  • 29
  • 69
  • The problem is that you're missing (or PHP - or NetBeans - can't find) your MySQL JDBC driver. I would recommend installing mysqlnd, the MySQL Native Driver: https://dev.mysql.com/downloads/connector/php-mysqlnd/ – paulsm4 Jul 07 '15 at 18:45

2 Answers2

4

Sorry for bad english!

Create a file named info.php with this content:

<?php
phpinfo();
?>

Save and put in your server root.
Access http://localhost/info.php
See what PDO Drivers are loaded (CTRL + F and type PDO drivers).
MySql PDO Driver need to be loaded like this: pdo mysql

If you don't see mysql on PDO drivers, you should edit php.ini and add extension=pdo_mysql.dll
Restart your Apache and try again in info.php

PS: Certify that you have the pdo_mysql.dll in your PHP extensions folder.

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
jflizandro
  • 622
  • 1
  • 8
  • 16
1

To anyone that don't know where is the extension folder. Go to PHP.ini and search for: extension_dir And how you can see in the image, thats the path.