开发者

mysql import script by query instead of bash

I have a file called script.sql

how can I run that file using q mysql query?

global $sql;
$res = $sql->query("\. /script.sql")or die(mysql_error());
开发者_开发知识库

gives:

#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 '\. /script.sql' at line 1


Read the file in, parse it into individual SQL statements, then execute them.


You can use mysql multi query but it's a PITA. A simpler solution is running all the queries found in the file (it might fail depending on the content):

foreach (explode(",", file_get_contents("/path/to/script.sql")) as $query) {
      $sql->query($query);
}

But the easiest and fastest is using mysqlimport. If you need to launch it from a script, you can use PHP to call the command line:

passhthru("mysql -hhost -uuser ppassword dbname < /path/to/script.sql");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜