Symfony PDO异常:驱动程序中发生异常:找不到mysql的驱动程序
我知道这是一个经常被问到的问题。但是我复习并阅读了所有的内容。不幸的是,我找不到问题的正确答案。我用的是symfony。我按照说明和教程学习。所有的步骤都进行得很顺利。我在终端中运行以下命令时没有任何问题:Symfony PDO异常:驱动程序中发生异常:找不到mysql的驱动程序,symfony,pdo,driver,Symfony,Pdo,Driver,我知道这是一个经常被问到的问题。但是我复习并阅读了所有的内容。不幸的是,我找不到问题的正确答案。我用的是symfony。我按照说明和教程学习。所有的步骤都进行得很顺利。我在终端中运行以下命令时没有任何问题: composer require symfony/orm-pack composer require --dev symfony/maker-bundle php bin/console doctrine:database:create php bin/console make:entity
composer require symfony/orm-pack
composer require --dev symfony/maker-bundle
php bin/console doctrine:database:create
php bin/console make:entity
php bin/console make:migration
php bin/console doctrine:migrations:migrate
php bin/console make:controller ProductController
使用上述命令,我可以创建一个数据库ProductEntity,一个Product的表
到目前为止,我认为与我的数据库的连接运行得很好。
然后在ProductController中,我使用了symphony网站上的代码:
// src/Controller/ProductController.php
namespace App\Controller;
// ...
use App\Entity\Product;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;
class ProductController extends AbstractController
{
/**
* @Route("/product", name="create_product")
*/
public function createProduct(): Response
{
// you can fetch the EntityManager via $this->getDoctrine()
// or you can add an argument to the action: createProduct(EntityManagerInterface $entityManager)
$entityManager = $this->getDoctrine()->getManager();
$product = new Product();
$product->setName('Keyboard');
$product->setPrice(1999);
$product->setDescription('Ergonomic and stylish!');
// tell Doctrine you want to (eventually) save the Product (no queries yet)
$entityManager->persist($product);
// actually executes the queries (i.e. the INSERT query)
$entityManager->flush();
return new Response('Saved new product with id '.$product->getId());
}
}
它给出了一个错误:没有找到像图中那样的驱动程序
我已经检查了env中的数据库url,它可以正常工作(我通过它创建了一个数据库和产品表)。我检查了phpinfo和pdo_mysql是否已启用,没有问题。
我已经按照这里的说明使用fixature测试了数据库连接,并且成功了,没有问题
你能帮帮我吗?
感谢guillaumesmo,我使用了symfony php-m。在这里,我看到了无法加载pdo_mysql库的错误。我记得,我的系统上安装了两个PHP版本。
我将PHP版本更新为PHP7.4.1,并删除了旧版本。它工作得很好。我不明白为什么我可以通过终端和固件连接和更新我的数据库,但不能使用EntityManagerInterface。无论如何,我现在可以工作了在我看来,驱动程序是在PHP CLI中启用的,而不是在web服务器PHP中启用的。您使用的是哪台服务器?我在terminal:symfony server:start中使用这个命令
symfony php-m
的输出是什么?它包括pdo和pdo_mysql吗?