【问题标题】:How can I enter data in the database?如何在数据库中输入数据?
【发布时间】:2019-11-02 06:54:30
【问题描述】:

我一直在尝试将数据保存在数据库中,但此错误不断出现:

(2/2) QueryException
SQLSTATE[HY000] [2002] Connection refused (SQL: insert into `stores` (`name`, `description`) values (El café de mi esquina , Es lindo))

这是控制器上的代码:

   * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */



     public function store()
        {
          $nuevoLocal= new Store();


          $nuevoLocal->name="El café de mi esquina ";
          $nuevoLocal->description="Es lindo";

$nuevoLocal-> save();

 }

网络:

 Route::get('/agregarNegocio', "storeController@store");

型号:

命名空间应用程序;

use Illuminate\Database\Eloquent\Model;

class store extends Model
{
    public $guarded =[];
    public $timestamps=false;

}

环境:

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=cafe

DB_USERNAME=root

DB_PASSWORD=root

数据库名称是cafe,它有一个表存储有ID(主键),名称和描述

如果放 var_dump($nuevoLocal);出口;就在 save() 之前,这是我得到的:

object(App\store)#158 (25) { ["guarded"]=> array(0) { } ["timestamps"]=> bool(false) ["connection":protected]=> NULL ["table":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(false) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(2) { ["name"]=> string(23) "El café de mi esquina " ["description"]=> string(8) "Es lindo" } ["original":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["events":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } }

【问题讨论】:

  • 错误是Connection refused,因此它是数据库的配置之一,主要是DB_HOST,但也可以是DB_PORT。试试DB_HOST=localhost
  • 您是否正确配置了 .env 文件。当你连接到你的数据库时,你使用Username = root & Password = root
  • 尝试修改 DB_HOST=localhost 但不起作用):
  • 确保你的 mysql 正在运行。好像mysql关了
  • mysql 正在运行!

标签: php database laravel localhost laravel-artisan


【解决方案1】:

尝试将DB_CONNECTION127.0.0.1 变为localhost。显然,在 Laravel 中使用 127.0.0.1 存在一些问题。

【讨论】:

  • 试过但没有结果):
  • 你是在使用宅基地流浪盒子进行开发吗?您确定root/root 是正确的用户信息吗?
【解决方案2】:

我遇到了同样的问题,结果发现端口不是标准的。从 SQL 服务器试试这个

USE master
GO
xp_readerrorlog 0, 1, N'Server is listening on', 'any', NULL, NULL, N'asc' 
GO

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 2019-07-10
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 2019-11-29
    相关资源
    最近更新 更多