This question already has answers here:
PDOException “could not find driver”
                                
                                    (38个答案)
                                
                        
                                3年前关闭。
            
                    
我正在尝试连接到数据库,但收到错误消息:

PDOException: could not find driver in E:\Drive\Dev\PHP\connect\index.php:3 Stack trace: #0 E:\Drive\Dev\PHP\connect\index.php(3): PDO->__construct('mysql:host=localhost;...', 'dbuser', 'dbpassword') #1 {main}


我使用XAMPP运行Apache和MySQL。我还配置了VirtualPorts并通过其别名访问所有网站,例如'mysite /'代替'localhost'

您能否检查下面的代码并帮助我解决问题。提前致谢!

<?php
    try{
        $pdo = new PDO('mysql:host=localhost;dbname=carbrands', 'dbuser', 'dbpassword');
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->exec('SET NAMES "utf8"');
    }
    catch(PDOException $e){
        $output = 'not connected';
        $e->getMessage();
        include 'output.html';
        exit();
    }

    $output = 'connected';
    include 'output.html';
?>

最佳答案

看起来您缺少正确的驱动程序。

检查此答案,因为它可能对您有帮助

Missing driver

关于php - 无法连接到数据库-PHP ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36510001/

10-12 07:18