为了账号安全,请及时绑定邮箱和手机立即绑定

第 15 集的问题 laracasts.com PHP 从业者

第 15 集的问题 laracasts.com PHP 从业者

PHP
手掌心 2023-12-15 15:09:09
这一集是关于隐藏密码的。我收到刷新本地主机的错误 - “无法找到驱动程序”如视频所示制作 config.php 后。连接.php<?phpclass Connection{    public static function make($config)    {        try {/*            return new PDO('mysql:host=localhost:3306;dbname=schema_test', 'name', 'password');*/            return new PDO(                $config['connection'].';dbname='.$config['name'],                $config['username'],                $config['password'],                $config['options']            );        } catch (PDOException $e) {            die($e->getMessage());        }    }}它适用于注释掉的版本,但在制作 config.php 并从那里获取信息后,我收到了错误。配置文件<?phpreturn [    'database' => [        'name' => 'schema_test',        'username' => 'name',        'password' => 'password',        'connection' => 'localhost:3306',        'options' => [],    ]];引导程序.php<?phprequire 'database/Connection.php';require 'database/QueryBuilder.php';$config = require 'config.php';return new QueryBuilder(    Connection::make($config['database']));另外,我在控制台中没有得到有关错误的其他信息。我已经尝试过对其他人有效的解决方案,例如: sudo apt-get install php-mysql我尝试重新安装一些与 php 相关的东西,但这也没有改变任何东西。<?phprequire 'database/Connection.php';require 'database/QueryBuilder.php';$config = require 'config.php';print_r($config);return new QueryBuilder(    Connection::make($config['database']));输出:Array ( [数据库] => Array ( [名称] => schema_test [用户名] => 用户 [密码] => 密码 [连接] => localhost:3306 [选项] => Array ( ) ) ) SQLSTATE[HY000] [1045] 用户“user”@“localhost”的访问被拒绝(使用密码:YES)
查看完整描述

2 回答

?
犯罪嫌疑人X

TA贡献2080条经验 获得超4个赞

您没有声明驱动程序,因此您得到'could not find driver'。此外,您还缺少命名 host= 参数。


要纠正此错误,请像这样更改 PDO 实例化:


return new PDO(

    'mysql:host='.$config['connection'].';dbname='.$config['name'],

    $config['username'],

    $config['password'],

    $config['options']

 );

您的$config数组没有问题,并且也被正确包含。</span>


查看完整回答
反对 回复 2023-12-15
?
动漫人物

TA贡献1815条经验 获得超10个赞

在 config.php 中,直接设置数组:


<?php


$config = [

    'database' => [

        'name' => 'schema_test',

        'username' => 'name',

        'password' => 'password',

        'connection' => 'mysql:host=localhost:3306',

        'options' => [],

    ]

];

在您的 bootstrap.php 中,请要求您的 config.php 文件如下:


require 'config.php';

并且不要忘记使用 $config['database']['name']...


查看完整回答
反对 回复 2023-12-15
  • 2 回答
  • 0 关注
  • 72 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信