我在laravel中遇到数据库连接错误。我要求检查我的代码。
我已经尝试删除php.ini文件中的分号(;)
route.php:
Route::get('/Insert', 'UserController@insert');
UserController.php:
public function insert(Request $request)
{
$result=DB::insert("insert into test (id, name, email) values(?,?,?)",['1','Nirav','n@gmail.com']);
$result=DB::select("select * from test");
print_r($result);
}
database.php:
DATABASE NAME : data
TABLE NAME : test
'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'database' => env('DB_DATABASE', "data"),
'username' => env('DB_USERNAME', "root"),
'password' => env('DB_PASSWORD', ""),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
],
.env文件:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=data
DB_USERNAME=root
DB_PASSWORD=
我正在数据库中插入记录。
答案 0 :(得分:1)
If you are on windows, and your php folder is not in your PATH, you have set the absolute directory in your php.ini
for example:
extension_dir = "C:/php7/ext"
and uncomment
extension=php_mysqli.dll
extension=php_pdo_mysql.dll
Restart apache2.4 and it should work.
I hope it helps.
答案 1 :(得分:0)
您需要为PHP安装PDO_MYSQL驱动程序
答案 2 :(得分:0)
转到wamp64/www/bin/php/php*
(其中*是您使用的php版本)
编辑文件php并通过删除分号取消注释此行:
;extension=pdo_pgsql to extension=pdo_pgsql
保存并重新启动Wamp / xampp服务器
再次检查您的.env
和config/database
。