Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tallaljamshed's avatar

using mysql load data query as a raw laravel query

Hi im trying to use this query

LOAD DATA INFILE 'c:/tmp/discounts.csv' 
INTO TABLE discounts 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

in laravel as raw query but that line terminating character actually takes the query on new line and it does not work .

$upload = DB::raw("LOAD DATA INFILE '$file' INTO TABLE '$request->table' FIELDS TERMINATED by ',' LINES TERMINATED BY '\n' IGNORE 1 ROWS");

this is the error

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''teachertest' FIELDS TERMINATED by ',' LINES TERMINATED BY ' ' IGNORE 1 ROWS' at line 1 
0 likes
6 replies
BryanK's avatar

Either don't use the single quotes around the table name, or use backtick `

$upload = DB::raw("LOAD DATA INFILE '$file' INTO TABLE `$request->table` FIELDS TERMINATED by ',' LINES TERMINATED BY '\n' IGNORE 1 ROWS");
tallaljamshed's avatar

thanks @bryank i just did that before looking at your answer. i thinks there is some progress but now im getting this error .

SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.

:(

BryanK's avatar
BryanK
Best Answer
Level 16

I suggest writing a helper function to get around that...something like this:

protected function loadDataFile(string $query)
    {

	if (config(database.default) != 'mysql') return;

        $pdo = DB::connection()->getPdo();
        $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
        $statement = $pdo->prepare($query,[\PDO::ATTR_CURSOR=>\PDO::CURSOR_SCROLL]);

        $exec = $statement->execute();
        if (!$exec) return $pdo->errorInfo();

        return $statement;

    }

Then call it ($this assumes method in same class):


$this->loadDataFile("LOAD DATA INFILE '$file' INTO TABLE `$request->table` FIELDS TERMINATED by ',' LINES TERMINATED BY '\n' IGNORE 1 ROWS");

tallaljamshed's avatar

@bryank thanks that worked perfectly. although i used fewer lines which i will show here for anyone else like me .

$file = base_path('public/csv/'.$request->csv);
$pdo = DB::connection()->getPdo();
$sql = "LOAD DATA LOCAL INFILE '$file' INTO TABLE $request->table FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 ROWS";
$pdo->exec($sql);
return redirect()->back();

and in config/database.php

'mysql' => [
            'driver' => 'mysql',
            ...
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
                PDO::MYSQL_ATTR_LOCAL_INFILE => true,
            ]) : [],

is there any problem with this way ? other then using $request->table directly in query .

this link was helpful https://laracasts.com/discuss/channels/laravel/load-data-infile

BryanK's avatar

Only problem I can see is always leaving that option enabled instead of setting it when it's actually needed. It's disabled by default for a reason (security).

As far as $request->table, that's fine as long as you let Laravel validate it first.

You could also check:

Schema::hasTable($request->table);
1 like

Please or to participate in this conversation.