my $sth = $dbh->prepare("SELECT snd.name, snd.country, m.item, m.eqty, m.price, m.line, m.prodct FROM buys as m JOIN Busers as snd ON snd.id = m.buyer WHERE CASE WHEN (SELECT SUM(status = ?) FROM buys) > 0 THEN m.status = ? ELSE m.status = ? OR m.status = ? OR m.status = ? END ORDER BY created ASC"); $sth->execute('sold', 'forsell', 'bugt', 'paid', 'inshop'); my $Data = $sth->fetchall_arrayref(); foreach my $Data (@$Data) { my ($name, $country, $item, $eqty, $price, $line, $prodct) = @$Data; $html = "

$name

$eqty, $price, $line,

$prodct $country "; my $json = encode_json( { msg => $html} ); print $q->header( -type => 'application/json' ),$json; }