简体   繁体   English

为什么缺少 PHP 7 PDO 驱动程序?

[英]Why are PHP 7 PDO drivers missing?

Okay, so.可以,然后呢。 I have PDO connection on MySQL database on local server.我在本地服务器上的MySQL数据库上有PDO连接。 This is the code for it这是它的代码

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";

try {
    $dbh = new PDO('localhost:host=$servername;dbname=test', $username, $password);
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>

When I open the page it gives me this error.当我打开页面时,它给了我这个错误。

Error!: could not find a driver

It is obvious that drivers are missing but I have no idea how to install them.很明显缺少驱动程序,但我不知道如何安装它们。 I already used我已经用过

sudo pacman -S php
sudo pacman -S php-sqlite
sudo pacman -S mysql

Maybe I forgot some.也许我忘记了一些。 Here is my /etc/php/php.ini file's content这是我的 /etc/php/php.ini 文件的内容

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

; If you wish to have an extension loaded automatically, use the following
; syntax:
;
;   extension=modulename
;
; For example:
;
;   extension=mysqli
;
; When the extension library to load is not located in the default extension
; directory, You may specify an absolute path to the library file:
;
   extension=/path/to/extension/mysqli.so
;
; Note : The syntax used in previous PHP versions ('extension=<ext>.so' and
; 'extension='php_<ext>.dll') is supported for legacy reasons and may be
; deprecated in a future PHP major version. So, when it is possible, please
; move to the new ('extension=<ext>) syntax.
;
;extension=bcmath
;extension=bz2
;extension=calendar
extension=curl
;extension=dba
;extension=enchant
;extension=exif
;extension=ftp
extension=gd
;extension=gettext
;extension=gmp
;extension=iconv
;extension=imap
;extension=intl
;extension=sodium
;extension=ldap
;extension=mysqli
;extension=odbc
;zend_extension=opcache
;extension=pdo_dblib
extension=pdo_mysql
extension=bz2.so
extension=mcrypt.so
extension=mysqli
;extension=pdo_odbc
;extension=pdo_pgsql
extension=pdo_sqlite
;extension=pgsql
;extension=pspell
;extension=shmop
;extension=snmp
;extension=soap
;extension=sockets
extension=sqlite3
;extension=sysvmsg
;extension=sysvsem
;extension=sysvshm
;extension=tidy
;extension=xmlrpc
;extension=xsl
extension=zip

It didn't let me upload the whole file so here is only part of it.它没有让我上传整个文件,所以这里只是其中的一部分。 If you need more details I will gladly add them in comments.如果您需要更多详细信息,我很乐意在评论中添加它们。 Maybe I am missing something obvious but I can't find it for a few days.也许我遗漏了一些明显的东西,但几天后我找不到它。

EDIT Sorry for not pointing this out.编辑抱歉没有指出这一点。 I am using Manjaro Linux OS我正在使用 Manjaro Linux 操作系统

When creating the connection, your DSN has localhost as the database type...创建连接时,您的 DSN 将localhost作为数据库类型...

$dbh = new PDO('localhost:host=$servername;dbname=test', $username, $password);

from the manual手册

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

Also as you use single quotes, the servername will not be replaced, so use此外,当您使用单引号时,服务器名称不会被替换,因此请使用

$dbh = new PDO("mysql:host=$servername;dbname=test", $username, 

I think PDO extensions are missing.我认为缺少 PDO 扩展。 Add the below extensions and restart your server添加以下扩展并重新启动您的服务器

For windows server -对于 Windows 服务器 -

extension=php_pdo.dll
extension=php_pdo_mysql.dll

For Linux server -对于 Linux 服务器 -

extension=pdo.so 
extension=pdo_mysql.so

尝试这个

sudo apt-get install php7-mysql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM