• Resolved gyclone

    (@gyclone)


    Greetings,

    I’ve installed WP DB Driver according to the provided instructions.

    I’ve added define( 'WPDB_DRIVER', 'pdo_mysql' ); to my wp-config.php.

    I’ve copied db.php to WordPress installation directory and wp-content/dp.php to the WordPress wp-content directory.

    I have a functions.php in my child theme with the following function:

    function twentytwelve_child_build_glossary(){
    
    		// Create PDO
    		$dbh = new PDO(DB_HOST, DB_USER, DB_PASSWORD);
    		$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    		$strSQL = "SELECT term, definition FROM <code>wp_glossary</code> ORDER BY term";
    
    		try {
    				// Prepare sql statement
    				$stmt = $dbh->prepare($strSQL);
    				$stmt->execute();
    				$result = $stmt->fetchAll();
    			} catch (Exception $e) {
    			$result = "PDO Failed with Error: $e";
    		}
    		return $result;
    	}

    I use an added shortcode to trigger the function, if that makes any difference.

    I get the following error when attempting to use the function:
    “Fatal error: Uncaught exception ‘PDOException’ with message ‘could not find driver’ in C:\xampp\htdocs\EOD\wp\wp-content\themes\twentytwelve-child\functions.php:15”

    Any idea what the issue might be.

    Thanks!

    https://wordpress.org/plugins/wp-db-driver/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter gyclone

    (@gyclone)

    Turns out I don’t even need the plugin to accomplish my goal. I thought it was required in order to use PDO from within WordPress, but apparently that is not the case.

    In case anyone stumbles on this, my revised function (below) works without using any plugins.

    `function twentytwelve_child_build_glossary(){

    $dbhost = DB_HOST;
    $dbname = DB_NAME;

    // Create PDO
    $dbh->_pdo = new PDO(“mysql:host=$dbhost, dbname=$dbname”, DB_USER, DB_PASSWORD);
    $dbh->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $strSQL = “SELECT term, definition FROM $dbname.wp_glossary ORDER BY term”;

    try {
    // Prepare sql statement
    $stmt = $dbh->_pdo->prepare($strSQL);
    $stmt->execute();
    $result = $stmt->fetchAll();
    } catch (Exception $e) {
    $result = “PDO Failed with Error: $e”;
    }

    return $result;
    }

    Sorry if I’ve wasted anyone’s time. Thanks!

    Plugin Author Marko Heijnen

    (@markoheijnen)

    Unsure what you are trying to do but I would say you are doing it wrong. You should never create your own database connection but instead using WPDB. If you that and you want to use PDO, then you can use this plugin.

    Thread Starter gyclone

    (@gyclone)

    Thank you for your response! I was unaware of WPDB at the time I posted this. I was trying to use a function I wrote for another program on a custom page and was getting a missing PDO driver error. I knew I had the PDO driver installed in my database because I use PDO all the time. It was late and I wasn’t thinking clearly and as a result, I wrongly assumed that my error was a result of WordPress not supporting PDO. I downloaded your plugin in hopes that it would solve my problem. It did not and I wrongly assumed I had failed to install the plugin properly. In fact, I had just made some silly errors in my code. Coincidentally, I stumbled upon WPDB the next morning and redid my function accordingly. Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Fatal error: Uncaught exception 'PDOException' with message 'could not find driv’ is closed to new replies.