Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl, DBI, MySQL and UTF-8 under Win32.

Status
Not open for further replies.

lenrobert

Programmer
Feb 24, 2005
13
GB
Hi,

finally I succeeded in inserting Unicode (namely utf8) strings into a MySQL database,
but what I still don't know is how to query the database to avoid getting
? instead of e.g. ?.
(áéö works somehow, since they are among latin1 (iso-8859-1) characters).

Can anyone help?

Thank you in advance.

Robert


Apache 2.0.53 (Win32)
Perl v5.8.6 built for MSWin32-x86-multi-thread
DBD::mysql 2.9008
DBI 1.48
MySQL Ver 14.7 Distrib 4.1.11, for Win32 (ia32)

SQL:
Code:
CREATE TABLE `practice` (
`id` int(11) NOT NULL auto_increment,
`String` varchar(40) character set utf8 collate utf8_bin NOT NULL default '',
PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

INSERT INTO `practice` (`id`, `String`) VALUES (1,'?°');

CGI:
Code:
#!c:/perl/bin/perl -wT

use strict;
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);
use DBI qw(data_string_desc);
use Encode;

binmode STDOUT, ":utf8";
binmode STDIN, ":utf8";

print "Content-type: text/plain\n\n";
print "\n--------- Receive the parameter ---------\n";

my $Nev = param('Nev');
$Nev = '' unless ($Nev);

if (utf8::valid($Nev)) {
	print "$Nev is valid utf-8\n";
	utf8::decode($Nev);  
}

print "After utf::decode : \$Nev = '$Nev' \n";

my $Database = "test";
my $Host = "localhost";
my $Username = "";
my $password = "";


my $dbh =  DBI->connect("DBI:mysql:$Database;$Host", $Username, $password,
    { RaiseError => 1, "mysql_enable_utf8" => 1 }
);

#$dbh->{mysql_enable_utf8} or die "couldn't init mysql_enable_utf8";

print "\n--------- The INSERT part ---------\n";
if ($Nev ne '') {
	print ("data_string_desc($Nev) == '".data_string_desc($Nev)."'\n");
	$Nev = "CONVERT(_utf8'$Nev' USING utf8)";
	my $hInsert = $dbh->prepare_cached("
        INSERT INTO `practice` (`String`) VALUES ($Nev)
    ");
    $hInsert->execute();
    $hInsert->finish ();
}

print "\n--------- The SELECT part ---------\n";


my $hSelect = $dbh->prepare("
    SELECT `String`
    FROM `practice`
    WHERE `id` = 1
");

$hSelect->execute();

my $char = ($hSelect->fetchrow)[0];
$hSelect->finish ();

decode("utf8", $char);
print "The result of SELECT == '$char'\n";



$dbh->disconnect();
exit (0);

HTML:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form method="post" action="form.cgi" accept-charset="utf-8"  enctype="application/x-[URL unfurl="true"]www-form-urlencoded"[/URL] target="formtarg">
<p>N?©v: <input type="text" name="Nev" size="25" /><br />
<input type="reset" value="Reset" /><input type="submit" value="Submit" /></p>
</form>
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top