laravelpdo::exec()期望参数1是字符串,object-given

t3psigkw  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(336)

我有这个函数,并且得到了一个错误 PDO::exec() :-

private function pdo()
{
    $path = File::get($_SERVER['DOCUMENT_ROOT'] . '\storage\uploadedReports\report.csv');
    try {
        $pdo = DB::connection()->getPdo();

        $loadCsv = $pdo->prepare("LOAD DATA LOCAL INFILE '" . $path . "'
                    INTO TABLE records 
                    FIELDS TERMINATED BY ',' enclosed by '\"' lines terminated by '\n' 
                    (percentage_artist_name, provider_name, sub_provider_name, @date, tune_code, tune_name,
                    category, sub_category, sub_type, downloads, total_revenue, @created_at, @updated_at)
                    SET `date` = date_format(@`date`,'%Y-%c-%d'), created_at = NOW(), updated_at = NOW()");

        $pdo->exec($loadCsv);
    } catch (\Exception $e) {
        dd($e->getMessage());
    }
}

我该怎么修?
提前谢谢。

bzzcjhmw

bzzcjhmw1#

方法$pdo->prepare返回一个statement对象。使用此对象可以调用execute方法。在execute方法中,如果查询有参数标记(?)?或:参数(名称)。
试试这个:

private function pdo()
{
    $path = File::get($_SERVER['DOCUMENT_ROOT'] . '\storage\uploadedReports\report.csv');
    try {
        $pdo = DB::connection()->getPdo();

        $loadCsv = $pdo->prepare("LOAD DATA LOCAL INFILE ?
                    INTO TABLE records 
                    FIELDS TERMINATED BY ',' enclosed by '\"' lines terminated by '\n' 
                    (percentage_artist_name, provider_name, sub_provider_name, @var1, tune_code, tune_name,
                    category, sub_category, sub_type, downloads, total_revenue, @created_at, @updated_at)
                    SET `date` = date_format(@`date`,'%Y-%c-%d'), created_at = NOW(), updated_at = NOW()");

        $loadCsv->execute([$path]);
    } catch (\Exception $e) {
        dd($e->getMessage());
    }
}

希望这有帮助。

8fq7wneg

8fq7wneg2#

private function pdo()
{
    try {
        $path = base_path('public\storage\uploadedReports\report.csv');
        $path = str_replace('\\', '/', $path);

        $pdo = DB::connection()->getPdo();
        $pdo->exec("LOAD DATA LOCAL INFILE '" . $path . "' INTO TABLE records FIELDS TERMINATED BY ',' enclosed by '\"' lines terminated by '\\n' (percentage_artist_name, provider_name, sub_provider_name, @`date`, tune_code, tune_name, category, sub_category, sub_type, downloads, total_revenue, @created_at, @updated_at) SET `date` = NOW(), created_at = NOW(), updated_at = NOW()");
    } catch (\Exception  $e) {
        dd($e->getMessage());
    }
}

相关问题