I'm trying to code a very simple MySQL SELECT query, a开发者_Go百科s 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 resultBUT:
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 foundI 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.
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
.
my $prep = $dbd->prepare($query) or die "ERR: " .$dbd->errstr;
$prep->bind_param( 1, $search_data, { TYPE => SQL_VARCHAR } );
$prep->execute( ) or die 'ERR : '.$prep->errstr;
精彩评论