Perl 或 MySQL 问题
我正在尝试编写一个非常简单的 MySQL SELECT 查询,如所附:
#
# MODULES
#
use strict;
use DBI;
use DBD::mysql;
#
# CONNECT
#
my $dbd = DBI->connect("dbi:mysql:dbname=$db_name;host=$host;mysql_server_prepare=1;",$db_user, $db_pwd)
or die 'No connection to dB : '.DBI::errstr;
#
# PREPARE
#
my $query = "SELECT * FROM $table WHERE $search_field = ? ";
my $prep = $dbd->prepare($query)
or die "ERR: " .$dbd->errstr;
#
# EXECUTE
#
$prep->execute( $search_data )
or die 'ERR : '.$prep->errstr;
#
# SHOW
#
while (my @data = $prep->fetchrow_array ) {
foreach my $line (@data) {
print $line.', ';
}
print "\n";
}
带有 TinyText 的搜索字段。 (但也尝试了 varchar,结果相同) 搜索字段编码为 utf8_unicode_ci (但也尝试了 latin_general_ci,相同的结果)
如果我使用 $search_data 的纯数字链运行请求(如 0124345):好的,我得到结果
如果我使用 $search_data 的纯文本链(如 AZERTY)运行请求:好的,我得到结果
但是:
如果我使用混合文本/数字运行请求(如 AZERTY01234):未找到记录,
如果我使用带有破折号、点等的混合文本运行请求(如 foo-bar):未找到记录
如果我使用带有破折号、点等的混合文本运行请求(如 foo+bar):找不到记录
我尝试了几种可能的修复,但没有成功。 (更改 SQL 数据类型,带或不带简单/双引号,没有准备好的查询...)
我不知道为什么以及如何解决它。
提前感谢一些提示。
I'm trying to code a very simple MySQL SELECT query, as per attached :
#
# MODULES
#
use strict;
use DBI;
use DBD::mysql;
#
# CONNECT
#
my $dbd = DBI->connect("dbi:mysql:dbname=$db_name;host=$host;mysql_server_prepare=1;",$db_user, $db_pwd)
or die 'No connection to dB : '.DBI::errstr;
#
# PREPARE
#
my $query = "SELECT * FROM $table WHERE $search_field = ? ";
my $prep = $dbd->prepare($query)
or die "ERR: " .$dbd->errstr;
#
# EXECUTE
#
$prep->execute( $search_data )
or die 'ERR : '.$prep->errstr;
#
# SHOW
#
while (my @data = $prep->fetchrow_array ) {
foreach my $line (@data) {
print $line.', ';
}
print "\n";
}
The search field with TinyText. (but tried also varchar, same result)
The search field is encoded utf8_unicode_ci (but tried also latin_general_ci, same result)
If I run the request with a number-only chain for $search_data (like 0124345): ok, I get the result
If I run the request with a text-only chain for $search_data (like AZERTY): ok, I get the result
BUT:
If I run the request with a mix text/numbers (like AZERTY01234) : No record found,
If I run the request with a mix text with dash, dot etc (like foo-bar): No record found
If I run the request with a mix text with dash, dot etc (like foo+bar): No record found
I tried several potential fix but no success.
(changing SQL data type, with and w/o simple/double quotes, no prepared query ...)
I have no idea why and how to get it solved.
Thx in advance for some hints.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来 Perl 可能会混淆字符串、数字及其算术。
解释 OReilly 的对 Perl DBI 进行编程,为了帮助确定绑定值中提供的数据类型,您可以在执行绑定时提供一个指定数据类型的附加参数。
对于您的情况,我会尝试类似以下的操作来强制绑定文本类型/
SQL_VARCHAR
。Looks like Perl might be confusing strings, numbers, and arithmetic thereof.
Paraphrasing OReilly's Programming the Perl DBI, to help work out what sort of data is being supplied in a bind value, you can supply an additional argument that specifies the datatype when you do the binding.
In your case, I'd try something like the following to force the binding of a text type /
SQL_VARCHAR
.