php中的mysql query,PHP:在一个mysql_query语句中进行多个SQL查询

So I have an SQL dump file that needs to be loaded using mysql_query(). Unfortunately, it's not possible to execute multiple queries with it.

-> It cannot be assumed that the mysql command-line client (mysql --help) is installed -- for loading the SQL file directly

-> It cannot be assumed that the mysqli extension is installed

/* contents of dump.sql, including comments */

DELETE FROM t3 WHERE body = 'some text; with semicolons; scattered; throughout';

DELETE FROM t2 WHERE name = 'hello';

DELETE FROM t1 WHERE id = 1;

The explode() below won't work because some of the dump content's values contain semicolons.

$sql = explode(';', file_get_contents('dump.sql'));

foreach ($sql as $key => $val) {

mysql_query($val);

}

What's the best way to load the SQL without modifying the dump file?

解决方案

You have more problem cases than just semicolons within strings.

Script builtin commands that cannot be executed by mysql_query(), like USE.

Statements that are not terminated with a semicolon, like DELIMITER.

Statements that contain semicolons, but not inside quotes, like CREATE PROCEDURE.

I don't know of an easy way to handle this task, without shelling out to the mysql command-line client. I realize you said you can't rely on that client being present, but without that client, you need a large amount of PHP code to parse the script and execute statements appropriately.

You may be able to find such code inside the phpMyAdmin product. However, that product is licensed under the GPL, so if you use any of the code, you must also license your own project under the GPL.

See also my answers to these related questions:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值