简体   繁体   中英

Illuminate\Database\QueryException: SQLSTATE[42S02]: Base table or view not found: 1146 Table &#03 (truncated

I've created an API using Laravel v5.8. This API has one endpoint that recieve some parameters and create 1 row on a database table. When I call to this endpoint using Postman, it works.

But... I've create an App using Laravel 5.8 and when this app try to call to that API endpoint sending the same parameters, and headers, this error is showed:

"Illuminate\\Database\\QueryException: SQLSTATE[42S02]: Base table or view not found: 1146 Table &#03 (truncated..."

That table should be 'api_log', but i can't see this name in the error message.

NOTE : this error happens in the local environment only.

The Laravel app is using GuzzleHttp to call to the API.

APP request:

$client = new \GuzzleHttp\Client();
$res = $client->request('POST', 'http://project.local/v1/api_log', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => $_ENV['API_TOKEN']
],
'form_params' => [
'json' => $json_request
]
]);

MySQL statement in API controller (no model is used):

$sql = "INSERT INTO api_log (user_id, environment, start_datetime, end_datetime, requested_api, processing_time, activity_id, ip_address, request_data, response_data, credits) VALUES (".$user_id.",'".$env."','".$start_datetime."','".$end_datetime."',".$requested_api.",".$processing_time.",'".$activity_id."','".$ip_address."','".$request_data."','".$response_data."',".$credits.")";            

$insert_log = DB::connection('mysql_log')->statement($sql);

I've already tried to use a model like this but it didn't work:

class ApiLog extends Model
{
    protected $connection = 'mysql_log';
    protected $table = 'api_log';
}

I'm using normal http://localhost/... domains for the API and APP. I've tried wrapping the API into a virtual host like http://project.local but the same error is showed.

Some clue? Thanks.

Well...after a lot of hours, I solved it watching the Laravel log file.

The problem was in .env file.

The .env file had this lines:

ALTER_CONNECTION=mysql_log
ALTER_HOST=127.0.0.1
ALTER_PORT=3306
ALTER_DATABASE=my_db
ALTER_USERNAME=root
ALTER_PASSWORD=my_pass

And I've changed it by:

ALT_CONNECTION=mysql_log
ALT_HOST=127.0.0.1
ALT_PORT=3306
ALT_DATABASE=my_db
ALT_USERNAME=root
ALT_PASSWORD=my_pass

I think these variables names were catched (I don't know why) so the DB config was failing.

Thank you all.

It's sad I cannot comment. But it seems like you haven't specified connection mysql_log in your database config file. In order to use $connection or DB::connection() you have to specify the connection in config/database.php

In case you done this, check if your values are put correctly in the file. It is wise to use a different constant for the database/username/password than the other connection you're running through the app.

'connections' => [
    // New connection
    'mysql_log' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('MYSQL_LOG_HOST', '127.0.0.1'),
        'port' => env('MYSQL_LOG_PORT', '3306'),        
        'database' => env('MYSQL_LOG_DATABASE', 'DB2'),
        'username' => env('MYSQL_LOG_USERNAME', 'DB2'),
        'password' => env('MYSQL_LOG_PASSWORD', 'PASS'),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],


    // Typical primary connection
    'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],
]

After you added the connection to the config, you can run php artisan config:cache to ensure the new configurations are cached properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM