Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

mysql - Using Template::Toolkit with array data from database

I don't know why my script works when I declare arrays in my script, but doesn't work when I try to get array data from a database.

Perl

use Template;

my $template = Template->new;

if ( $info ) {

    my $select = $DBH->prepare("SELECT FOO, BAR, MOO FROM tble WHERE CONCAT(FOO, ', ', BAR, ', ', MOO) LIKE ?");
    $select->execute('%' . $info . '%');
    $names = $select->fetchall_arrayref();
    foreach $names ( @$names ) {
        ( $variable1, $variable2, $variable3 ) = @$names;
    }
}

my $templ = <<START_HTML;
<!DOCTYPE html">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1
+" />
<title>Untitled Document</title>
</head>

<body>

[% FOREACH name IN list %]
    <li>
      <div class='inforno'>
        <img src='inforno'>
      </div>
      <div class='inforno'>
        <a href='#' class='inforno'>[% name %]</a>
      </div>
      <span class='inforno'>
        <a href='#' class='inforno'>Edit user</a>
      </span>
    </li>
    [% END %]

</body>
    </html>
    START_HTML

$template->process($templ, { list => @$names })
        or die $template->error;

output

    ARRAY(0x2030674)
    ARRAY(0x2030634)
    ARRAY(0x2030618)

When I run I get that error. I want to get the array data from database.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

fetchall_arrayref returns a reference to an array that has an element for each row returned by the query. Those elements are references to arrays with an element per column.

You are missing code to loop over the rows returned.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...