dqrnsg6439 2016-08-22 22:34 采纳率: 100%
浏览 97

未捕获的PDOException:找不到驱动程序

I have just installed an Ubuntu Server 16.04 with Nginx, and i have installed php in this way:

sudo apt-get install php7.0-cli php7.0-cgi php7.0-fpm php7.0-curl

And for MySql i have installed percona, in this way:

apt-get install percona-server-server-5.7 percona-server-client-5.7

but I can't understand why i can't use PDO in my php script, this is my code:

$db = new PDO("mysql:dbname=db;host=localhost;charset=utf8", "username", "password");

I get this error:

Uncaught PDOException: could not find driver

I have searched online and I have found that i have to uncomment this line in Windows in php.ini:

;extension=php_pdo_mysql.dll

or this line on Linux:

;extension=php_pdo_mysql.so

I have checked my php.ini in:

/etc/php/7.0/fpm/php.ini

and i have a .dll file and not a .so file, I can't also understand why I have a dll instead of a so. Anyway enabling that .dll file and restarting php nothing change. Anyone can help me?

Thanks

  • 写回答

1条回答 默认 最新

  • douqiang7976 2016-08-23 14:19
    关注

    Installing the mysql client/server packages doesn't effect PHP. PHP still needs the bindings to talk to your mysql server, specifically in your case the PDO mysql driver.

    The package is probably going to be called php7.0-pdo-mysql depending on which repositories are configured your Ubuntu package manager. If you're using PHP 7 on Ubuntu 16.04 it's more than likely going to be a meta package that includes php7.0-mysql. Here's what I got from aptitude on 16.04 using the Ondrej PPA.

    aptitude show php7.0-pdo-mysql

    Package: php7.0-mysql                    
    State: installed
    Automatically installed: no
    Version: 7.0.9-1+deb.sury.org~trusty+1
    Priority: optional
    Section: php
    Maintainer: Debian PHP Maintainers 
    Architecture: amd64
    Uncompressed Size: 514 k
    Depends: php-common (>= 1:35), ucf, php7.0-common, libc6 (>= 2.15)
    Provides: php-mysqli, php-mysqlnd, php-pdo-mysql, php7.0-mysqli, php7.0-mysqlnd, php7.0-pdo-mysql
    Description: MySQL module for PHP
     This package provides the MySQL module(s) for PHP. 
    

    The php7.0-pdo-mysql is the one we're interested in for loading the mysql driver for PDO in PHP. So make sure to search your package manager to find the correct package before you attempt installing, but there should be a similar package for your distro/repositories.

    Once installed you'll need to restart php-fpm, something like sudo service php7.0-fpm restart and then check your phpinfo() to make sure the pdo_mysql driver is now loaded.

    You shouldn't need to edit your php.ini manually when installing through your package manager. Also, adding the extension=pdo_mysql.so line to your php.ini has no effect if the extension doesn't exist. And obviously, trying to load dll won't work on linux, those are Windows-only libraries.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题