convert php to perl

-1

I need to convert the following PHP code to perl (using CGI)

if(isset($_POST['product_detail'])){
        $pid=$_POST['pid'];
        $sql="SELECT * FROM products WHERE product_id='$pid'";
        $run_query=mysqli_query($conn,$sql);
        $row=mysqli_fetch_array($run_query);
        $pro_id=$row['product_id'];
        $image=$row['product_image'];
        $title=$row['product_title'];
        $price=$row['product_price'];
        $desc=$row['product_desc'];
        $tags=$row['product_keywords'];
        $stock=$row['Stock'];
        echo "
                <div class='row'>
                    <div class='col-md-6 pull-right'>
                        <img src='assets/prod_images/$image' style='width:250px;height:300px;'>
                    </div>
                    <div class='col-md-6'>
                        <div class='row'> <div class='col-md-12'><h1>$title</h1></div></div>
                        <div class='row'> <div class='col-md-12'>Precio:<h3 class='text-muted'>$price</h3></div></div>
                        <div class='row'> <div class='col-md-12'>Descripción:<h4 class='text-muted'>$desc</h4></div></div><br><br>
                        <div class='row'> <div class='col-md-12'>Unidades disponibles:<h4 class='text-muted'>$stock</h4></div></div><br><br>
                        <div class='row'> <div class='col-md-12'>Etiquetas:<h4 class='text-muted'>$tags</h4></div></div>
                        <button pid='$pro_id' class='product btn btn-danger'>Añadir al carrito</button>
                    </div>
                </div>
        ";
    }
    
asked by Carlos Saz 17.10.2017 в 02:06
source

1 answer

0

I advise you to look at a basic perl tutorial and the documentation of the CGI and DBD :: mysql module before asking questions of this type.

Even so I leave the code here: P (not tested)

use CGI;
use DBD::mysql;

my $q = new CGI;

my $product_detail = $q->param('product_detail');
my $pid = $q->param('pid');

if ($product_detail) {
    my $db = connect_db();
    my $sql="SELECT * FROM products WHERE product_id='$pid'";
    my $sth = $db->prepare($sql);
    $sth->execute() or die "imposible ejecutar la consulta $sql\n";;
    my $row = $sth->fetchrow_hashref();
    $sth->finish();
    my $db->disconnect();
    my $pro_id=$row->{'product_id'};
    my $image=$row->{'product_image'};
    my $title=$row->{'product_title'};
    my $price=$row->{'product_price'};
    my $desc=$row->{'product_desc'};
    my $tags=$row->{'product_keywords'};
    my $stock=$row->{'Stock'};

    my $html = <<EOT;
                <div class='row'>
                    <div class='col-md-6 pull-right'>
                        <img src='assets/prod_images/$image' style='width:250px;height:300px;'>
                    </div>
                    <div class='col-md-6'>
                        <div class='row'> <div class='col-md-12'><h1>$title</h1></div></div>
                        <div class='row'> <div class='col-md-12'>Precio:<h3 class='text-muted'>$price</h3></div></div>
                        <div class='row'> <div class='col-md-12'>Descripción:<h4 class='text-muted'>$desc</h4></div></div><br><br>
                        <div class='row'> <div class='col-md-12'>Unidades disponibles:<h4 class='text-muted'>$stock</h4></div></div><br><br>
                        <div class='row'> <div class='col-md-12'>Etiquetas:<h4 class='text-muted'>$tags</h4></div></div>
                        <button pid='$pro_id' class='product btn btn-danger'>Añadir al carrito</button>
                    </div>
                </div>
EOT
print $html;    
}

sub connect_db {
    my ($dbname,$dbhost,$dbuser,$dbpass) = ('nombredb', 'localhost', 'usuariodb', 'passdb');
    my $db = DBI->connect("DBI:mysql:$dbname:$dbhost", "$dbuser", "$dbpass") or die "Imposible conectar con la DB";
    $sth->execute() or die "conecta a la db";
    $sth->finish;
    return $db;
} 
    
answered by 31.10.2017 в 07:01