我刚刚安装了Debian Lenny与Apache, MySQL和PHP,我收到一个PDOException无法找到驱动程序。

这是它所引用的特定代码行:

$dbh = new PDO('mysql:host=')。DB_HOST。”;dbname = '。Db_name, db_user, db_pass)

DB_HOST、DB_NAME、DB_USER和DB_PASS是我已经定义的常量。它在生产服务器(以及我之前的Ubuntu server设置)上运行良好。

这与我的PHP安装有关吗?

在网上搜索也没有帮助,我得到的都是专家交流和例子,但没有解决方案。


当前回答

对于使用MAMP的人,

如果你有

激活扩展= php_pdo_mysql.dll 设置你的扩展所在的正确路径,(例如:extension_dir = "C:\MAMP\bin\php\php7.4.1\ext\")

...你仍然不能在phpinfo()上看到pdo_mysql扩展,然后确保你从你的web服务器的文档根调用phpinfo(),而不是从其他地方。

如果你一直在其他地方调用它,那么你可能编辑了错误的php.ini文件:

Go to the document root of your web server (the path can be found in Mamp > preferences > Web server) Create a file named info.php in this folder, open it and paste <?php phpinfo(); ?> then save the file. Open the info.php you've just created in your browser (typically, http://localhost/info.php) Now search for "Loaded Configuration File" in the page and look at the path of your php.ini file --> It's probably a different php.ini that the one you've been originally editing. So open this file and make the edits (uncomment.., etc) Restart your server Check info.php again, the extension pdo_mysql should appear.

如果没有,如果你以前像我一样改变了你的服务器的文档根路径,我建议你重新设置为“C:\MAMP\htdocs”,并把你的网站放在这里,因为这真的解决了我的问题。

希望这能帮助到一些人。

其他回答

更新:新版本应该使用php-sqlite3包而不是php5-sqlite。所以,如果你使用的是最新的ubuntu版本,请使用这个:

sudo apt-get install sqlite php-sqlite3

问题的原始答案在这里:

sudo apt-get install sqlite php5-sqlite
sudo /etc/init.d/apache2 restart

如果你的phpinfo()没有显示pdo_sqlite行(在我的情况下,在我的Ubuntu服务器上),你只需要运行上面的行,然后你就可以开始了。

在我的情况下(Windows XP + Apache2.2 + PHP 5.4.31),我改变了PHPIniDir在httpd.conf来解决这个问题:

来自:

"C:\php5\" 

to:

"C:/php5/"

我强烈推荐mysqllnd而不是mysql,因为你会有很多问题,如数字转换和位类型计算的问题与mysql扩展。

在ubuntu上使用以下命令安装mysqllnd:

sudo apt-get install php5-mysqlnd

如果你读了上面所有的答案,它仍然不工作…

确保PHP PDO连接字符串正常。不像我的:

$dbh = new PDO(' ' mysql:host='。DB_HOST。”;dbname = '。Db_name, db_user, db_pass)

错误消息中没有没有找到驱动程序的信息。

在重新安装所有可能的PDO和MySQL库后,我发现有“at start of my connection string”。

我有同样的错误共享主机使用cPanel。如果安装了mysqlnd,则默认安装pdo_mysql扩展。

但事实并非如此,我必须启用nd_pdo_mysql扩展来使用PDO。我使用PHP选择器GUI来更改它,但这是因为我懒得设置SSH连接。

O,忘记添加服务器是Apache和MariaDB(经常忘记,尽管MariaDB与MySql具有相同的父级,但在底层,一些功能可能会略有不同)。

PHP 8.0 与 Laravel 8.0 相关: