为什么在Perl DBI中不能使用分号将多个MySQL查询连接在一起? [英] Why doesn't joining multiple MySQL queries with semicolons work with Perl DBI?

查看:94
本文介绍了为什么在Perl DBI中不能使用分号将多个MySQL查询连接在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 DBI 将值插入两个单独的MySQL表中.我试图通过在两个查询之间插入一个;来将两个有效的INSERT查询合并为一个:

I would like to insert values into two separate MySQL tables using DBI. I tried to combine two working INSERT queries into one by inserting a ; between them:

$dbh->do(q{
    INSERT INTO `testA`(test) values('testvalue111');
    INSERT INTO `testB`(test) values('testvalue222');
});

但是我总是会出错:

"INSERT INTO`testB`(test)values('testvalue222 ...中的语法错误

Syntactic error in "INSERT INTO `testB`(test) values('testvalue222 ..."

如果我将查询分为两个单独的do调用,则它可以工作.但是合并的查询在phpMyAdmin中可以正常工作.为什么在Perl中失败?

If I separate the queries into two separate do calls, it works. But the combined query works fine in phpMyAdmin. Why does it fail in Perl?

推荐答案

您需要在connect调用中设置一个选项才能启用此功能.来自文档:

You need to set an option in the connect call to enable this. From the docs:

mysql_multi_statements
从MySQL 4.1开始,可以使用此选项启用对多个用分号(;)分隔的语句的支持.如果还启用了服务器端的预处理语句,则启用此选项可能会导致问题.

mysql_multi_statements
As of MySQL 4.1, support for multiple statements seperated by a semicolon (;) may be enabled by using this option. Enabling this option may cause problems if server-side prepared statements are also enabled.

默认情况下它是禁用的,可能应该保持未设置状态-这是SQL注入的主要途径(您应该阅读一下,尤其是在不使用绑定的情况下).

It is disabled by default, and should probably remain unset - that's a large avenue for SQL injection (which you should be reading about especially if you're not using binds).

这篇关于为什么在Perl DBI中不能使用分号将多个MySQL查询连接在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆