How to solve 500 Error on Yii Db connection

2019-09-21 01:05发布

问题:

I have uploaded yii framework into my server. i'm using string for connecting db is

return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=smargavc_Konnections',
    'username' => 'smargavc_Konnect',
    'password' => '******',
    'charset' => 'utf8',
];

Here, Wheather dont know db is connected or not.

When i trying in postman to access the rest call, getting 500 errro

回答1:

500 Error is for CODE error. Not connection error.

To check connection you can simply go,

print_r(Yii::app()->db); // Yii 1.1.x framework

Regards



回答2:

If you want to check the db connection then use getIsActive() method of yii\db\Connection Class. For more info check this link



回答3:

I had response configuration in web.php. It made problem of 500 Error.

 'response' => [
            'class' => 'yii\web\Response',
            'on beforeSend' => function ($event) {
                $response = $event->sender;
                if ($response->data !== null && !empty(Yii::$app->request->get('suppress_response_code'))) {
                    $response->data = [
                        'success' => $response->isSuccessful,
                        'data' => $response->data,
                    ];
                    $response->statusCode = 200;
                }
            },
        ],

I had this in my configuration. I dont know the reason. But it solve my issue



标签: php yii yii2