PHP PDO“找不到驱动程序”错误与php 5.5.21 / Apache 2.4 / MySQL 5.6.22在Wi


PHP PDO "could not find driver" error with php 5.5.21 / Apache 2.4 / MySQL 5.6.22 in windows

我有一个简单的带有PDO的.php文件

<?php
// DB connection info
$host = "localhost";
$user = "root";
$pwd = "1234";
$db = "registration";
try{
    $conn = new PDO( "mysql:host=$host;dbname=$db", $user, $pwd);
    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $sql = "CREATE TABLE registration_tbl(
                id INT NOT NULL AUTO_INCREMENT,
                PRIMARY KEY(id),
                name VARCHAR(30),
                email VARCHAR(30),
                date DATE)";
    $conn->query($sql);
}
catch(Exception $e){
    die(print_r($e));
}
echo "<h3>Table created.</h3>";
?>

当我执行这个php文件时,我可以看到这条错误消息。

PDOException Object ( [message:protected] => could not find driver [string:Exception:private] => [code:protected] => 0 [file:protected] => C:'Apache24'htdocs'registration'createtable.php [line:protected] => 8 [trace:Exception:private] => Array ( [0] => Array ( [file] => C:'Apache24'htdocs'registration'createtable.php [line] => 8 [function] => __construct [class] => PDO [type] => -> [args] => Array ( [0] => mysql:host=localhost;dbname=registration [1] => root [2] => 1234 ) ) ) [previous:Exception:private] => [errorInfo] => ) 1

所以我取消注释并根据有关类似问题的其他问题和答案设置"php.ini"文件。

extension=php_pdo_mysql.dll
extension_dir = "c:/php/ext" (absolute Path)

但是,情况没有特别的变化。还是找不到司机。

但是,当我输入"php -m"和"php -i"时,我可以找到PDO是启用的。

php -m

[PHP Modules]
bcmath
calendar
Core
ctype
date
dom
ereg
filter
ftp
hash
iconv
json
libxml
mcrypt
mhash
mysqlnd
odbc
pcre
PDO
pdo_mysql
Phar
Reflection
session
SimpleXML
SPL
standard
tokenizer
wddx
xml
xmlreader
xmlwriter
zip
zlib

php -i

pcre.backtrack_limit => 1000000 => 1000000
pcre.recursion_limit => 100000 => 100000
PDO
PDO support => enabled
PDO drivers => mysql
pdo_mysql
PDO Driver for MySQL => enabled
Client API version => mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb10
57292d73b928b8c5c77 $
Phar
Phar: PHP Archive support => enabled
Phar EXT version => 2.0.2
Phar API version => 1.1.1
SVN revision => $Id: cc0fad28eb9ea42466f756c3b5fc22c764e32690 $
Phar-based phar archives => enabled
Tar-based phar archives => enabled
ZIP-based phar archives => enabled
gzip compression => enabled
bzip2 compression => disabled (install pecl/bz2)
OpenSSL support => disabled (install ext/openssl)

为什么他们找不到PDO驱动程序?请帮助我。谢谢。

对我来说,我发现扩展名(extension=php_pdo_mysql.dll)在php.ini中被注释掉了。

从行的开头删除了";",重新启动了服务和XAMPP,一切都很好。

首先查找 pdo_mysql,然后在 phpinfo() 中查找 php.ini 路径。可能你只是取消了注释错误的php.ini.

对我有用的是:

  • 首先 - 取消注释扩展
  • 第二个 - 更改 extension_dir 变量 php.ini