PDO support for multiple queries (PDO_MYSQL, PDO_MYSQLND)

后端 未结 7 2279
长发绾君心
长发绾君心 2020-11-21 06:03

I do know that PDO does not support multiple queries getting executed in one statement. I\'ve been Googleing and found few posts talking about PDO_MYSQL and PDO_MYSQLND.

7条回答
  •  说谎
    说谎 (楼主)
    2020-11-21 06:31

    A quick-and-dirty approach:

    function exec_sql_from_file($path, PDO $pdo) {
        if (! preg_match_all("/('(\\\\.|.)*?'|[^;])+/s", file_get_contents($path), $m))
            return;
    
        foreach ($m[0] as $sql) {
            if (strlen(trim($sql)))
                $pdo->exec($sql);
        }
    }
    

    Splits at reasonable SQL statement end points. There is no error checking, no injection protection. Understand your use before using it. Personally, I use it for seeding raw migration files for integration testing.

提交回复
热议问题